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

时间:2022-01-29 18:56:00

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.

我们有一个xml节点“item”,其属性为“style”,即“Header1”。但是这种风格可以改变。我们有一个名为Header1的属性集,它定义了在xsl:fo生成的PDF中的外观。

This works (the use-attribute-sets is mentioned inline, in the fo:table-cell node):

这是有效的(在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>

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:属性,因为@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>

Does anyone know why? And how we could achieve this, preferably without long xsl:if or xsl:when stuff?

有谁知道为什么?我们如何才能实现这一点,最好不要长xsl:if或xsl:什么时候?

3 个解决方案

#1


5  

From http://www.w3.org/TR/xslt#attribute-sets

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

通过在xsl:element,xsl:copy [...]或xsl:attribute-set元素上指定use-attribute-sets属性来使用属性集。

From http://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element

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

并且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).

所以,很明显它不能是AVT(动态定义)。

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.

注意:关于文字结果元素,规范说:也可以通过在文字结果元素上指定xsl:use-attribute-sets属性来使用属性集。允许AVT很少含糊不清。假设没有。

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.

关于第二个示例:使用该模板,您将在结果树中添加“xsl:use-attribute-sets”属性。它不是XSLT处理器所能解释的。

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.

那么,解决方案是什么?你必须摆脱“xsl:use-attribute-sets”。为“@style”应用模板规则并在那里生成所需的属性。

#2


0  

Try:

尝试:

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

#3


0  

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:

使用变量定义样式,变量为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']"/>

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

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

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

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

#1


5  

From http://www.w3.org/TR/xslt#attribute-sets

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

通过在xsl:element,xsl:copy [...]或xsl:attribute-set元素上指定use-attribute-sets属性来使用属性集。

From http://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element

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

并且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).

所以,很明显它不能是AVT(动态定义)。

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.

注意:关于文字结果元素,规范说:也可以通过在文字结果元素上指定xsl:use-attribute-sets属性来使用属性集。允许AVT很少含糊不清。假设没有。

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.

关于第二个示例:使用该模板,您将在结果树中添加“xsl:use-attribute-sets”属性。它不是XSLT处理器所能解释的。

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.

那么,解决方案是什么?你必须摆脱“xsl:use-attribute-sets”。为“@style”应用模板规则并在那里生成所需的属性。

#2


0  

Try:

尝试:

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

#3


0  

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:

使用变量定义样式,变量为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']"/>

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

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

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

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