Reputation: 1499
I am trying to make template that prints all atributes values.
<xsl:template match="@*">
<xsl:value-of select="."/>
</xsl:template>
<!-- override default template for text nodes -->
<xsl:template match="text()">
</xsl:template>
But the output is empty. Why?
Input
<hello name="Dominik">
<help level="Hard"/>
</hello>
Expected output
DominikHard
Real output
EMPTY
Upvotes: 1
Views: 35
Reputation: 70648
Assuming you just have those two templates, and nothing else, the reason you are not getting the correct output is because of XSLT's built-in template rules, which apply when there is not a matching template in your XSLT. In your case, you have no template matching any element, so the following built-in template applies
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
This skips over the element and processes the child nodes. It does not select any attributes though, and so your attribute matching template does not apply.
The solution is to change your template that matches text()
to matching node()
instead, and include code to select child attributes for that. (Text nodes obviously have no attributes or child, so this template will just ignore text nodes too)
Try this XSLT instead
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="yes" />
<xsl:template match="node()">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
Note how there is no need for a template matching @*
because the built-in template rules now apply for that, and that outputs the value when matched.
Upvotes: 2