XSLT template not applying to newly-created elements



I am using XSLT 2.0. Inside a xsl:template (template-1), I use xsl:analyze-string to create new span elements with an xml:lang attribute. I have a second template (template-2) that adds a class attribute to elements that contain a xml:lang attribute. I cannot figure out how to have the newly-created span elements created by the first template be processed by the second. How can I have the second template operate on results from the first?


Example:


Input: <p>The base form of a noun is technically called a <span xml:lang="sa-Latn">prātipadika</span> (प्रातिपदिक).</p>


Desired Output: <p>The base form of a noun is technically called a <span xml:lang="sa-Latn" class="lang-sa-latn">prātipadika</span> (<span xml:lang="sa-Deva" class="lang-sa-deva">प्रातिपदिक</span>).</p> This correct output has a final span with both xml:lang and class attributes.


Stylesheet Output: <p>The base form of a noun is technically called a <span xml:lang="sa-Latn" class="lang-sa-latn">prātipadika</span> (<span xml:lang="sa-Deva">प्रातिपदिक</span>).</p> This incorrect output is missing class="sa-lang-deva" on the final span.


(This stylesheet helps to work around the deficient CSS support of a certain eBook reader.)




Here is my stylesheet:



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://ift.tt/tCZ8VR"
xmlns="http://ift.tt/lH0Osb"
xpath-default-namespace="http://ift.tt/lH0Osb"
xmlns:xml="http://ift.tt/1ikcaie"
xmlns:epub="http://ift.tt/1io14WJ">

<xsl:output method="xhtml" encoding="utf-8" indent="no"/>

<xsl:template match="/">
<html xmlns="http://ift.tt/lH0Osb" xmlns:epub="http://ift.tt/1io14WJ">
<xsl:apply-templates/>
</html>
</xsl:template>

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

<!-- Template-1: Add xml:lang attribute to Devanagari text. -->
<xsl:template match="element()/text()">
<xsl:variable name="textValue" select="."/>
<xsl:analyze-string select="$textValue" regex="([&#x0900;-&#x097f;]+)((\s+[&#x0900;-&#x097f;]+)*)">
<xsl:matching-substring>
<span xml:lang="sa-Deva"><xsl:value-of select="."/></span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>

<!-- Template-2: Add lang-* class attribute when xml:lang attribute present. -->
<xsl:template match="*[@xml:lang]">
<xsl:call-template name="addClass">
<xsl:with-param name="newClass">lang-<xsl:value-of select="@xml:lang"/></xsl:with-param>
</xsl:call-template>
</xsl:template>

<!-- Add a class attribute to an element. -->
<xsl:template name="addClass">
<xsl:param name="newClass"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="class"><xsl:value-of select="normalize-space(concat(@class, ' ', lower-case($newClass)))"/></xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

No comments:

Post a Comment