Tim Siefert
Tim Siefert

Reputation: 606

XPath for 0-* child level

I have following XML-File:

<?xml version="1.0" encoding="UTF-16"?>
<Export>
    <Folder ID="1004">
        <Object ID="124" Name="NameABC" />
    </Folder>
    <Folder ID="1016">
       <Folder Name="B">
            <Object ID="124" Name="Name1" />
                <Folder Name="A">
                    <Object ID="121244" Name="Name2" />
                    <Object ID="122134" Name="Name12" />
                    <Folder Name="KS">
                        <Object ID="667" Name="Name43" />
                    </Folder>
                </Folder>
        </Folder>
    </Folder>
</Export>

Now I need to get all <Object>'s under which have a ID and a NAME. Sometimes there is 0 Folder under the first Folder with the ID 1016 and sometimes 4.

I need to use a XPath expression. I hope you can help me. Thanks.

Upvotes: 1

Views: 238

Answers (2)

seriyPS
seriyPS

Reputation: 7112

//folder[@ID='some_id' and @Name='some_name']/descendant::*

See:

xpath axes

Upvotes: 1

andyb
andyb

Reputation: 43823

<xsl:apply-templates select="//Object">
   <xsl:value-of select="@ID"/> - <xsl:value-of select="@Name"/>
</xsl:apply-templates>

will match all <Object> elements and output the ID and Name attribute values.

The descendant selector is (from the documentation) // is short for /descendant-or-self::node()/

Upvotes: 0

Related Questions