Math
Math

Reputation: 38

XSLT/Xpath : matching multiple elements with redundant Xpath starters

I have an XML that look like this :

<parent>
    <child1/>
    <child2/>
    <child3/>
    <child4/>
    <child5/>
    <child6/>
</parent>

And I have a template that match only specific children looking like this :

<xsl:template match="parent/child1|parent/child4|parent/child6/>

Is there a way to write the "parent" tag only once and then write the "child" tags in a simplified expression witch would look like this ?

<xsl:template match="parent/(child1|child4|child6)"/>

Upvotes: 1

Views: 326

Answers (2)

Tim C
Tim C

Reputation: 70618

In this particular example you could just write this

 <xsl:template match="child1|child4|child6"/>

You would only need to worry about adding parent if there were other child1 elements in the XML with a different parent that you didn't want to remove. If this wasn't the case, you could instead write this...

<xsl:template match="(child1|child4|child6)[parent::parent]"/>

EDIT: As mentioned in comments, this only works in XSLT 3.0 and above.

Upvotes: 1

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

you can try

<xsl:template match="parent/*[self::child1 or self::child4 or self::child6]"/>

Upvotes: 1

Related Questions