如何组合 xsl:attribute 和 xsl:use-attribute-sets 以有条件地使用属性集?

发布于 2024-09-05 07:09:14 字数 1338 浏览 4 评论 0原文

我们有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞美少年 2024-09-12 07:09:14

来自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>

http://www.w3.org/TR/xslt#复制

<!-- Category: instruction -->
<xsl:copy
  use-attribute-sets = qnames>
  <!-- Content: template -->
</xsl:copy>

因此,很明显它不能是 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

<!-- Category: instruction -->
<xsl:element
  name = { qname }
  namespace = { uri-reference }
  use-attribute-sets = qnames>
  <!-- Content: template -->
</xsl:element>

And http://www.w3.org/TR/xslt#copying

<!-- Category: instruction -->
<xsl:copy
  use-attribute-sets = qnames>
  <!-- Content: template -->
</xsl:copy>

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.

三人与歌 2024-09-12 07:09:14

尝试

<fo:table-cell xsl:use-attribute-sets="{@style}">

Try:

<fo:table-cell xsl:use-attribute-sets="{@style}">
恏ㄋ傷疤忘ㄋ疼 2024-09-12 07:09:14

使用一个变量来定义 style,一个用于 true 的变量,一个用于 false 的变量,以及一个使用字符串连接动态引用任一变量的变量:

<xsl:variable name="style">
  <xsl:value-of select="concat(boolean(@style),boolean(not(@style) ) )"/>
</xsl:variable>

<xsl:variable name="falsetrue" select="'foo'"/>
<xsl:variable name="truefalse" select="'bar'"/>
<!--...-->


<xsl:value-of select="//xsl:variable/@select[../@name='style']"/>

或者您可以让模板自行匹配并使用“style”的值调用它们:

<xsl:template name="Header1" match="xsl:template[@name='Header1']"/>

<xsl:template name="Header2" match="xsl:template[@name='Header2']"/>

Use a variable to define style, a variable for true, a variable for false, and a variable to reference either one dynamically using string concatenation:

<xsl:variable name="style">
  <xsl:value-of select="concat(boolean(@style),boolean(not(@style) ) )"/>
</xsl:variable>

<xsl:variable name="falsetrue" select="'foo'"/>
<xsl:variable name="truefalse" select="'bar'"/>
<!--...-->


<xsl:value-of select="//xsl:variable/@select[../@name='style']"/>

Or you can have the templates match themselves and call them using the value of "style":

<xsl:template name="Header1" match="xsl:template[@name='Header1']"/>

<xsl:template name="Header2" match="xsl:template[@name='Header2']"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文