【问题标题】:hyphenation character in xslt attribute (xsl-fo)xslt 属性中的连字符 (xsl-fo)
【发布时间】:2016-04-20 09:16:28
【问题描述】:

我认为这是一个非常简单的问题。但是虽然我构建了非常花哨的xslt转换,但这个简单的转换是我无法解决的。

问题是: 我想根据 xml 数据向 xsl-fo 节点添加属性。这些属性通常带有连字符。如何在 xsl:attributes 不喜欢连字符的情况下使用 xslt 转换添加这些。


在 xml 节点中,我有两个属性(名称和值) 示例:name="font_size", value="7pt"

<Report>
  <text content="I am a text">
    <blockFormat name="font_size" value="7pt" />
  </text>
</Report>

(我知道这不是想要的,因为您想使用样式等。这只是一个简化问题的示例)


现在我想制作一个 xsl-fo 块,我想通过使用 xsl-function xsl:attribute 将该属性放在块元素中

<fo:block>
  <attribute name="{replace(@name,'_','-')}" select="@value" />
....
</fo:block>

改造后要实现的目标

<fo:block font-size="7pt">
....
</fo:block

它不起作用,我认为这是因为在 xslt 中我不能在属性名称中放置连字符,但在 fo-attribute 中需要它。

有没有办法为此使用 xsl:attribute 函数?

如果不是,您建议采用哪种解决方法。

感谢您的帮助!!!!

【问题讨论】:

  • "我认为这是因为在 xslt 中我不能在属性名称中添加连字符" 当然可以。这究竟是如何失败的?您收到错误消息吗?您确定您使用的是 XSLT 2.0 处理器吗?

标签: xslt attributes special-characters xsl-fo hyphen


【解决方案1】:

有 1000 种方法可以做到这一点,这里有一种(我没有对您的 Report 元素做任何事情):

输入:

<Report>
    <text content="I am a text">
        <blockFormat name="font_size" value="7pt" />
    </text>
</Report>

XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    version="1.0">
    <xsl:template match="Report">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text">
        <fo:block>
            <xsl:apply-templates select="blockFormat/@*"/>
            <xsl:value-of select="@content"/>
        </fo:block>
    </xsl:template>
    <xsl:template match="@name">
        <xsl:attribute name="{translate(.,'_','-')}">
            <xsl:value-of select="ancestor::blockFormat/@value"/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="@value"/>
</xsl:stylesheet>

输出:

<Report>
   <fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format" font-size="7pt">I am a text</fo:block>
</Report>

【讨论】:

  • 太棒了,确实太简单了。我没有使用翻译功能,这给我带来了麻烦。现在我可以轻松处理了!!!
【解决方案2】:

使用@select 代替@value

<fo:block>
  <attribute name="{replace(@name,'_','-')}" select="@value" />
....
</fo:block>

https://www.w3.org/TR/xslt20/#creating-attributes

此外,您需要使用 XSLT 2.0 或 3.0 才能使用 @select。如果您使用的是 XSLT 1.0,则必须使用 xsl:attribute/xsl:value-of/@select

(如果您还显示了错误的结果,这也有助于理解您的问题。)

【讨论】:

  • 对不起,我在输入问题时犯了一个错误。我使用“选择”。你不可能知道。问题是我没有得到错误的输出,而是转换时失败。也许它只是在 xsl-fo 处理步骤中。我不能很快看到。我会试着找出答案。
  • &lt;attribute 应该是 &lt;xsl:attribute(除非这也是一个错字?)。如果这不是问题,您将生成什么作为 fo:block 开始标签?
猜你喜欢
  • 2018-06-22
  • 2017-08-13
  • 2012-03-15
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 2010-10-18
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多