Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check type of node in XSL template

Tags:

xslt

xpath-1.0

Is it possible to check the type of a node I matched with a template inside the same template? In case it is, how can I do it? For example I would like to do something like this:

<xsl:template match="@*|node()">
    <xsl:choose>
        <xsl:when test="current() is an attribute">
        <!-- ... -->
        </xsl:when>
        <xsl:when test="current() is an element">
        <!-- ... -->
        </xsl:when>
        <xsl:otherwise>
        <!-- ... -->
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
like image 963
hielsnoppe Avatar asked Jan 02 '13 08:01

hielsnoppe


People also ask

What is node in xsl?

Use the XSLTransform node to transform an XML message to another form of message, according to the rules provided by an XSL (Extensible Stylesheet Language) style sheet, and to set the Message domain, Message set, Message type, and Message format for the generated message.

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit) # (Digit, zero shows as absent)

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

What is mode in xsl template?

mode. The mode attribute allows an element as specified by its Qualified Names to be processed multiple times, each time producing a different result. If <xsl:template> does not have a match attribute, it cannot have a mode attribute.


1 Answers

Take a look at this answer here, as this should give you the information you need:

Difference between: child::node() and child::*

This gives the following xsl:choose to test all the nodes, including the document node.

<xsl:choose>
  <xsl:when test="count(.|/)=1">
    <xsl:text>Root</xsl:text>
  </xsl:when>
  <xsl:when test="self::*">
    <xsl:text>Element </xsl:text>
    <xsl:value-of select="name()"/>
  </xsl:when>
  <xsl:when test="self::text()">
    <xsl:text>Text</xsl:text>
  </xsl:when>
  <xsl:when test="self::comment()">
    <xsl:text>Comment</xsl:text>
  </xsl:when>
  <xsl:when test="self::processing-instruction()">
    <xsl:text>PI</xsl:text>
  </xsl:when>
  <xsl:when test="count(.|../@*)=count(../@*)">
    <xsl:text>Attribute</xsl:text>
  </xsl:when>
</xsl:choose>
like image 165
Tim C Avatar answered Oct 17 '22 00:10

Tim C