1

I need to select a Children node that contains only boy Child nodes.

From this XML:

<Parent>
    <Name>Mary</Name>
    <Sex>female</Sex>
    <Children>
        <Child>
            <Name>Joe</Name>
            <Sex>boy</Sex>
        </Child>
        <Child>
            <Name>Harry</Name>
            <Sex>boy</Sex>
        </Child>
        <Child>
            <Name>Sue</Name>
            <Sex>girl</Sex>
        </Child>
        <Child>
            <Name>Connie</Name>
            <Sex>girl</Sex>
        </Child>
        <Child>
            <Name>Kim</Name>
            <Sex>other</Sex>
        </Child>
    </Children>
</Parent>

I need this result:

<Children>
    <Child>
        <Name>Joe</Name>
        <Sex>boy</Sex>
    </Child>
    <Child>
        <Name>Harry</Name>
        <Sex>boy</Sex>
    </Child>
</Children>

This xpath gives me the right Child nodes - but not inside a Children node.

//Children/Child[Sex='boy']

This xpath gives me the Children node I want - but with all the Child nodes inside - not just the boys.

//Children[Child/Sex='boy']

Can it be done with xpath?

Soren Werk
  • 61
  • 5

1 Answers1

1

XPath can give you single nodes only, not XML like this

<Children>
    <Child>
        <Name>Joe</Name>
        <Sex>boy</Sex>
    </Child>
    <Child>
        <Name>Harry</Name>
        <Sex>boy</Sex>
    </Child>
</Children>

To receive what you asking for you should use other tools, not a pure XPath.
For mode details see here or here

Prophet
  • 32,350
  • 22
  • 54
  • 79