web analytics

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

Options

codeling 1599 - 6654
@2016-01-25 14:19:19

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>
...
@2019-06-17 09:37:15

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>
@2019-06-17 10:14:41

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">
@2019-06-17 10:16:11

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),

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com