如何组合 xsl:attribute 和 xsl:use-attribute-sets 以有条件地使用属性集?
我们有一个 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>
但这不行(使用 xsl:attribute,因为属性 @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>
有谁知道为什么?我们如何才能实现这一点,最好没有长 xsl:if 或 xsl:when 的东西?
We have an xml node "item" with an attribute "style", which is "Header1". This style can change however. We have an attribute set named Header1 which defines how this should look in a PDF, generated through xsl:fo.
This works (the use-attribute-sets is mentioned inline, in the fo:table-cell node):
<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>
But this doesn't (using xsl:attribute, because the attribute @style can also be Header2 for example). It doesn't generate an error, the PDF is created, but the attributes aren't applied.
<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>
Does anyone know why? And how we could achieve this, preferably without long xsl:if or xsl:when stuff?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自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
和 http://www.w3.org/TR/xslt#复制
因此,很明显它不能是 AVT(动态定义)。
注意:关于文字结果元素,规范说:还可以通过在文字结果元素上指定 xsl:use-attribute-sets 属性来使用属性集。 这种情况很少见对于允许 AVT 含糊其辞。假设没有。
关于第二个示例:使用该模板,您可以将“xsl:use-attribute-sets”属性添加到结果树中。它不由 XSLT 处理器解释。
那么,解决办法是什么呢?您必须摆脱“xsl:use-attribute-sets”。为“@style”应用模板规则并在其中生成所需的属性。
From http://www.w3.org/TR/xslt#attribute-sets
Attribute sets are used by specifying a use-attribute-sets attribute on xsl:element, xsl:copy [...] or xsl:attribute-set elements
From http://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element
And http://www.w3.org/TR/xslt#copying
So, it's clear it can't be an AVT (dynamicly defined).
Note: About literal result element, the specification say: Attribute sets can also be used by specifying an xsl:use-attribute-sets attribute on a literal result element. It's rare vague about allowing AVT. Assume no.
About second example: with that template you're adding a "xsl:use-attribute-sets" attribute into the result tree. It's not iterpreted by the XSLT processor.
Then, what's the solution? You have to get rid of "xsl:use-attribute-sets". Apply a template rule for "@style" and generate the desired attributes there.
尝试:
Try:
使用一个变量来定义
style
,一个用于true
的变量,一个用于false
的变量,以及一个使用字符串连接动态引用任一变量的变量:或者您可以让模板自行匹配并使用“style”的值调用它们:
Use a variable to define
style
, a variable fortrue
, a variable forfalse
, and a variable to reference either one dynamically using string concatenation:Or you can have the templates match themselves and call them using the value of "style":