Reputation: 77
I have a file with a <fraction d="2" n="1">
tag. I need to convert it to ½
I tried doing it with this:
<xsl:template match="fraction">
<xsl:text>&frac</xsl:text>
<xsl:value-of select="@n"/>
<xsl:value-of select="@d"/>
<xsl:text>;</xsl:text>
</xsl:template>
but I get an error- possibly due to referencing &frac
Upvotes: 2
Views: 395
Reputation: 243529
There is no need for DOE (and its use isn't just bad taste!):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:fractions>
<frac14>¼</frac14>
<frac12>½</frac12>
<frac34>¾</frac34>
<frac18>⅛</frac18>
<frac38>⅜</frac38>
<frac58>⅝</frac58>
<frac78>⅞</frac78>
</my:fractions>
<xsl:variable name="vFracs" select="document('')/*/my:fractions/*"/>
<xsl:template match="fraction">
<xsl:value-of select="$vFracs[name()= concat('frac', current()/@n, current()/@d)]"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<fraction d="2" n="1"/>
the wanted, correct result is produced:
½
Upvotes: 2
Reputation: 31610
This seems to work for me (xslt 1.0):
<xsl:template match="/">
<xsl:text disable-output-escaping="yes">&frac</xsl:text>
<xsl:value-of select="@n"/>
<xsl:value-of select="@d"/>
<xsl:text>;</xsl:text>
</xsl:template>
Upvotes: 2