【发布时间】:2014-08-05 09:56:01
【问题描述】:
我正在构建一个相当复杂的 XSLT 以生成一些 HTML 标记。
我的目标之一是使用一些模板“扩展”生成的标记的class 属性。
不幸的是,它不起作用,因为 XSLT 标记 <xsl:attribute> 只能“设置”属性。不要操纵现有的。
当我尝试时,原始属性被擦除。
这是一个小复制:
XML:
<node>
<item value="1" type="abc"/>
<item value="20" type="zxy"/>
</node>
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/node/item">
<p class="{@type}">
<xsl:call-template name='rule1' />
<xsl:call-template name='rule2' />
</p>
</xsl:template>
<xsl:template name='rule1'>
<xsl:attribute name='class'>
<xsl:choose>
<xsl:when test="@value mod 2 = 0">alpha</xsl:when>
<xsl:otherwise>omega</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
<xsl:template name='rule2'>
<xsl:attribute name='class'>
<xsl:choose>
<xsl:when test="@value mod 10 = 0">beta</xsl:when>
<xsl:otherwise>gamma</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
我想输出:
<?xml version="1.0" encoding="utf-8"?>
<p class="abc omega"/>
<p class="zxy beta alpha"/>
但它输出
<?xml version="1.0" encoding="utf-8"?>
<p class="omega"/>
<p class="beta"/>
是否可以保留原始属性,或者在实用程序模板中检索它以重新使用它?
【问题讨论】:
-
你不能设置一个 xsl:variable 以便以后重用它吗? (例如将@type 设置为变量,然后将新属性设置为“concat(variablename, appends)?我对 xslt 还不是很满意,但我认为这会起作用。
-
xslt 中的一个变量不能设置多次。这在我上面的 sn-p 中并不明显,但我将链接模板以检查多个业务规则并将多个类应用于我的节点。如果我使用变量,我仍然必须在每个模板调用中声明至少一个变量。
-
我已更新我的 sn-p 以包含多个模板
-
它不能动态更新,但每次在xml中找到与该模板匹配的节点时,每次调用模板时都可以重新填充它。
-
事实上你不能像
<xsl:value-of select="concat(@type, ' appends')"/>那样做,@type(或者如果你更深入的话,是父节点的 XPath)仍然是他们以前的内容吗?
标签: xslt