Use wildcard expressions * and @* to process all XML elements and attributes in XSLT Options

codeling Posts: 1089 Points: 4569
Posted: Monday, January 25, 2016 2:19:19 PM

In XSLT, you can use wildcard expressions such as * and @* to process all the elements and attributes in an XML document. Use of these expressions allows us to apply this stylesheet to any XML document, regardless of the tags it uses. Because we use these wildcard expressions, we have to use the name( ) function to get the name of the element or attribute we’re currently working with.

<xsl:template match="*">
<xsl:variable name="element-name">
<xsl:value-of select="name()"/>
</xsl:variable>
...

codeling Posts: 1089 Points: 4569
Posted: Monday, June 17, 2019 9:37:15 AM

we can use conditional logic and the expression count(@*) &gt; 0 to determine whether a given element has attributes.

<xsl:if test="count(@*) &gt; 0">
...
</xsl:if>

codeling Posts: 1089 Points: 4569
Posted: Monday, June 17, 2019 10:14:41 AM

To determine whether the current element has children element or not:

<xsl:if test="count(*) &gt; 0">
...
</xsl:if>

Or perhaps you want to check whether the current element has a child named payment or not:

<xsl:when test="count(self::*/payment) &gt; 0">

codeling Posts: 1089 Points: 4569
Posted: Monday, June 17, 2019 10:16:11 AM

Alternatively, you could do this

<xsl:when test="*[2]">

i.e, is there an element in the second position (that saves counting all elements, when you only really want to check there is more than one),

Users browsing this topic
Guest