Zhao
Zhao

Reputation: 2193

XSLT "match" and "if" meaning

I'm really new to XSLT and I'm struggling to understand some codes. Following is the code I don't understand:

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

and:

    <xsl:if test="*">

What does it mean when you test a * symbol?

Upvotes: 1

Views: 236

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66783

The first template is the basis for an identity transform, and often used as the basis for a "push style" stylesheet in which the default behavior is to simply copy the input into the output. More specific templates are added in order to customize the behavior and produce different content.

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

This template will match any attribute @* and any node (element, comment, text, or processing instruction) node(), and is shorthand for: *|comment()|text()|processing-instruction()

When any of those items are matched, the template first copies the matched item, and then invokes xsl:apply-templates for any attributes or node() children of the context item. In the case of an attribute, text(), comment(), or processing-instruction() there will be no attributes or child node(). For an element, it could match either of those things. Unless there is a more specific template, it will simply get matched with this template and copy the matched item and continuing processing its attributes and children (if any).

In the case of the xsl:if:

<xsl:if test="*">

That tests to see if there are any child elements (relative from the context node). If there are any, then the condition is satisfied and the test evaluates to true() and whatever is placed inside the if will be applied.

For more information about the abbreviated syntax, see the spec: https://www.w3.org/TR/xpath/#path-abbrev

Upvotes: 2

Related Questions