user2183943
user2183943

Reputation: 77

xslt conversion fraction

I have a file with a <fraction d="2" n="1"> tag. I need to convert it to &frac12; 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

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

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>&#188;</frac14>
  <frac12>&#189;</frac12>
  <frac34>&#190;</frac34>
  <frac18>&#8539;</frac18>
  <frac38>&#8540;</frac38>
  <frac58>&#8541;</frac58>
  <frac78>&#8542;</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

Pawel
Pawel

Reputation: 31610

This seems to work for me (xslt 1.0):

<xsl:template match="/">
  <xsl:text disable-output-escaping="yes">&amp;frac</xsl:text>
  <xsl:value-of select="@n"/>
  <xsl:value-of select="@d"/>
  <xsl:text>;</xsl:text>
</xsl:template>

Upvotes: 2

Related Questions