【问题标题】:Xml content did not recognized by template模板无法识别 XML 内容
【发布时间】:2013-11-25 14:35:57
【问题描述】:

我有一个带有标签的 xml,其中包含一个带有 html 内容的属性。我需要将此 html 转换为 xsl-fo 。这是我的 xslt 代码:

  <xsl:template match ="rtf">
    <fo:block-container>
      <fo:block>
        <xsl:call-template name ="ConvertHtmlToXslfo">
          <xsl:with-param name ="content">
            <xsl:value-of select ="@rtfAsHtml" disable-output-escaping ="yes"/>
          </xsl:with-param>
        </xsl:call-template>
      </fo:block>
    </fo:block-container>
  </xsl:template>

  <xsl:template name="ConvertHtmlToXslfo">
    <xsl:param name ="content"></xsl:param>
    <fo:block-container>

      <xsl:apply-templates select="msxsl:node-set($content)"/> <!--here is the problem-->

    </fo:block-container>
  </xsl:template>

  <xsl:template match ="div">
    <fo:block-container>
        <!--more code here-->
    </fo:block-container>
  </xsl:template>

  <xsl:template match ="p">
    <fo:block>
        <!--more code here-->
    </fo:block>
  </xsl:template>

  <xsl:template match ="span">
    <fo:inline>
        <!--more code here-->
    </fo:inline>
  </xsl:template>

但是有一个问题,当在这个 html 内容上调用 apply-templates 时。相关模板无法识别。

这是带有 Html 属性的 xml 标签:

<rtf rtfAsHtml="&lt;div &gt;&lt;p &gt;&lt;span&gt;Hi!&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;"/>

知道如何将此 Html 标签转换为 xsl-fo 吗?

谢谢!

【问题讨论】:

  • 您的输入文件是否有任何命名空间声明?此外,如果您能够将“rtf”匹配为文档节点,那么在我看来,您的输入是 RTF 而不是 HTML。
  • @MathiasMüller。不,我的输入没有任何命名空间。而且,我的输入是一个 xml 标记,其中包含从 rtf 转换的 html。但它是html。

标签: xslt xsl-fo


【解决方案1】:

问题是您的 xml/html 内容被转义了。您尝试应用禁用输出转义,但这仅适用于写入输出文件或流。因此,您实际上只是针对您的模板提交未转义的属性内容,这确实没有多大作用。

不确定您使用的是哪种 XSLT 解析器,但如果您使用 Saxon,您可以尝试应用 saxon:parse:

http://saxonica.com/documentation9.4-demo/html/extensions/functions/parse.html

这确实要求属性的内容格式正确。如果没有,你可以试试:

http://saxonica.com/documentation9.4-demo/html/extensions/functions/parse-html.html

HTH!

【讨论】:

  • 谢谢。你说的对。但我使用的是 Microsoft XslCompiledTransform。
  • @jacob 不幸的是,.Net 中的这些解析函数似乎没有等价物。当然,您可以恢复到 Saxon.Net。其他替代方法是创建您自己的 XPath 扩展函数。我在 stackoverflow 上找到了这个,展示了如何:stackoverflow.com/questions/2320505/…
  • 可能可以使用类似于此答案的内容,其中在 XSLT 中实现了“转义 XML 解析器”:stackoverflow.com/a/20150995/407651
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多