我正在使用XSLT 2.0。在xsl:template
(template-1)内,我xsl:analyze-string
用来创建span
具有xml:lang
属性的新元素。我有第二个模板(template-2),它class
向包含xml:lang
属性的元素添加了属性。在我的样式表中,第一个模板创建的新创建的span元素未被第二个模板处理。如何解决此问题,并使第二个模板对第一个模板的结果进行操作?
例子:
输入: <p>The base form of a noun is technically called a <span xml:lang="sa-Latn">prātipadika</span> (प्रातिपदिक).</p>
所需的输出:<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>
此正确的输出具有span
带有xml:lang
和class
属性的最终输出。
样式表输出:Final上<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>
缺少此错误的输出。class="sa-lang-deva"
span
(样式表产生的其他类有助于解决某些电子书阅读器对CSS的支持不足的问题。)
这是我的样式表:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:epub="http://www.idpf.org/2007/ops">
<xsl:output method="xhtml" encoding="utf-8" indent="no"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<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="([ऀ-ॿ]+)((\s+[ऀ-ॿ]+)*)">
<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>
您需要将由创建的节点捕获analyze-string
到变量中,然后将模板应用于它们。您可以使用模板模式来避免无限递归。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:epub="http://www.idpf.org/2007/ops">
<xsl:output method="xhtml" encoding="utf-8" indent="no"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="@*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<!-- Template-1: Add xml:lang attribute to Devanagari text. Note no
mode attribute, so only applies in default mode. -->
<xsl:template match="text()">
<xsl:variable name="textValue" select="."/>
<xsl:variable name="nodes" as="node()*">
<xsl:analyze-string select="$textValue" regex="([ऀ-ॿ]+)((\s+[ऀ-ॿ]+)*)">
<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:variable>
<!-- apply templates to generated nodes, but with a mode that stops
this template from firing again -->
<xsl:apply-templates select="$nodes" mode="no-deva" />
</xsl:template>
<!-- Template-2: Add lang-* class attribute when xml:lang attribute present. -->
<xsl:template match="*[@xml:lang]" mode="#all">
<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()" mode="#current"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我对原始XSLT所做的更改是:
mode="#all"
到标识模板和“模板2”,因此它们适用于所有模式,并添加mode="#current"
到相关apply-templates
指令中,因此它们使用当前模式进行递归。analyze-string
起来,<xsl:variable>
以捕获其生成的节点。apply-templates
这些节点使用不同的模式由于我(故意地)没有添加mode="#all"
到模板1中,因此它仅在初次通过时匹配,而在期间不匹配apply-templates mode="no-deva"
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句