我们有一个xml节点"item",其属性为"style",即"Header1".但是这种风格可以改变.我们有一个名为Header1的属性集,它定义了在xsl:fo生成的PDF中的外观.
这是有效的(在fo:table-cell节点中内联提到了use-attribute-sets):
<xsl:template match="item[@type='label']">
<fo:table-row>
<fo:table-cell xsl:use-attribute-sets="Header1">
<fo:block>
<fo:inline font-size="8pt" >
<xsl:value-of select="." />
</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
但这不是(使用xsl:属性,因为@style属性也可以是Header2).它不会生成错误,会创建PDF,但不会应用属性.
<xsl:template match="item[@type='label']">
<fo:table-row>
<fo:table-cell>
<xsl:attribute name="xsl:use-attribute-sets">
<xsl:value-of select="@style" />
</xsl:attribute>
<fo:block>
<fo:inline font-size="8pt" >
<xsl:value-of select="." />
</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么?我们如何实现这一目标,最好不要长xsl:if或xsl:什么时候?
小智 6
来自http://www.w3.org/TR/xslt#attribute-sets
通过在xsl:element,xsl:copy [...]或xsl:attribute-set元素上指定use-attribute-sets属性来使用属性集.
来自http://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element
<!-- Category: instruction -->
<xsl:element
name = { qname }
namespace = { uri-reference }
use-attribute-sets = qnames>
<!-- Content: template -->
</xsl:element>
Run Code Online (Sandbox Code Playgroud)
和http://www.w3.org/TR/xslt#copying
<!-- Category: instruction -->
<xsl:copy
use-attribute-sets = qnames>
<!-- Content: template -->
</xsl:copy>
Run Code Online (Sandbox Code Playgroud)
所以,很明显它不能是AVT(动态定义).
注意:关于文字结果元素,规范说:也可以通过在文字结果元素上指定xsl:use-attribute-sets属性来使用属性集.允许AVT很少含糊不清.假设没有.
关于第二个示例:使用该模板,您将"xsl:use-attribute-sets"属性添加到结果树中.它不是XSLT处理器所能解释的.
那么,解决方案是什么?你必须摆脱"xsl:use-attribute-sets".为"@style"应用模板规则并在那里生成所需的属性.