xml - Xpath for contains rather than exact match - Stack Overflow

admin2025-04-28  2

I'm using the following xpath to return all "path" elements based on the element "group" value:

//recent/item/location[group="rgrp1"]/path

This works well but the match im looking for isnt always "rgrp1" but it will ALWAYS start with "rgrp". What i would actually like to do is rather than looking for an exact match of "rgrp1", i would rather return all path elements where the group element contains "rgrp". ive tried using contains() but im not having any luck with it. below is a sample of the structure of the file:

<?xml version="1.0" encoding="utf-8"?>
<recent max="16">
    <item>
        <context>reports</context>
        <type>spreadsheet</type>
        <subtype>workbook</subtype>
        <location>
            <group>rgrp1</group>
            <hierarchy>h3</hierarchy>
            <node>n38</node>
            <path>//Reports/Test1</path>
        </location>
    </item>
    <item>
        <context>files</context>
        <type>frameset</type>
        <location>
            <group>fgrp6</group>
            <hierarchy>h1</hierarchy>
            <node>n205</node>
            <path>//Reports/Test2</path>
        </location>
    </item>
</recent>

any help would be greatly appreciated.

I'm using the following xpath to return all "path" elements based on the element "group" value:

//recent/item/location[group="rgrp1"]/path

This works well but the match im looking for isnt always "rgrp1" but it will ALWAYS start with "rgrp". What i would actually like to do is rather than looking for an exact match of "rgrp1", i would rather return all path elements where the group element contains "rgrp". ive tried using contains() but im not having any luck with it. below is a sample of the structure of the file:

<?xml version="1.0" encoding="utf-8"?>
<recent max="16">
    <item>
        <context>reports</context>
        <type>spreadsheet</type>
        <subtype>workbook</subtype>
        <location>
            <group>rgrp1</group>
            <hierarchy>h3</hierarchy>
            <node>n38</node>
            <path>//Reports/Test1</path>
        </location>
    </item>
    <item>
        <context>files</context>
        <type>frameset</type>
        <location>
            <group>fgrp6</group>
            <hierarchy>h1</hierarchy>
            <node>n205</node>
            <path>//Reports/Test2</path>
        </location>
    </item>
</recent>

any help would be greatly appreciated.

Share Improve this question asked Jan 9 at 18:29 Graham ChandlerGraham Chandler 1931 gold badge5 silver badges16 bronze badges 3
  • 2 Could you show how you are using contains()? – SiKing Commented Jan 9 at 18:32
  • 1 i think i actually figured it out, naturally my very next attempt after posting: //item[location/group[contains(., 'rgrp')]]/location/path this seems to do the trick – Graham Chandler Commented Jan 9 at 18:36
  • 1 I've downvoted your question: in future, it you write code and it doesn't work, show us the code so that we can tell you what you did wrong. – Michael Kay Commented Jan 9 at 20:31
Add a comment  | 

2 Answers 2

Reset to default 1

This XPath,

/recent/item/location[group[contains(., 'rgrp')]]/path

will select all path elements with the noted heritage and a sibling group element whose string value contains a 'rgrp' substring.

//item[location/group[contains(., 'rgrp')]]/location/path

this ended up working for me

转载请注明原文地址:http://anycun.com/QandA/1745780586a91196.html