【问题标题】:Reading breaks in XML file when using CDATA and XSL使用 CDATA 和 XSL 时读取 XML 文件中的中断
【发布时间】:2015-04-22 10:13:41
【问题描述】:

我在使用CDATA 时尝试读取换行符,但运气不佳。有什么我做错了吗?我不断得到:

This Picture was created by <br /> Type-Style back in 2007.作为输出,根本不识别换行

XML

<s xmlns:b="http://crownpublishing.com/imprint/crown-business/">
    <b:bio>
        <b:about>
        <![CDATA[This Picture was created by <br /> Type-Style back in 2007. ]]>
        </b:about>
    </b:bio>
</s>

XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://crownpublishing.com/imprint/crown-business/" version="1.0">
<xsl:output cdata-section-elements="about"/>
<xsl:template match="/">
    <xsl:value-of select="s/b:bio/b:about"/>
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 输出也应该是 CDATA 部分吗?

标签: xml xslt xslt-1.0


【解决方案1】:

此答案假定您无法更改输入文档。使用字符串替换将br 字符串转换为实际的换行符。在 XSLT 1.0 中,进行字符串替换的标准方法是编写一个递归模板

XSLT 样式表

命名模板是从 Mads 的答案 here 中无耻地窃取的,而后者又是从 Dave Pawson 借来的。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:b="http://crownpublishing.com/imprint/crown-business/" version="1.0"
    exclude-result-prefixes="b">

    <xsl:output method="xml"  indent="yes"/>

    <xsl:template match="/">
    <result>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="/s/b:bio/b:about"/>
          <xsl:with-param name="replace" select="'&lt;br /&gt;'" />
          <xsl:with-param name="with" select="'&#10;'"/>
        </xsl:call-template>
    </result>
    </xsl:template>

    <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$with"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text"
select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

XML 输出

<?xml version="1.0" encoding="utf-8"?>
<result>
        This Picture was created by 
 Type-Style back in 2007. 
        </result>

试试这个解决方案online here

【讨论】:

    【解决方案2】:

    源文档中的文字换行符将传递到输出。试试:

    <s xmlns:b="http://crownpublishing.com/imprint/crown-business/">
        <b:bio>
            <b:about>
            <![CDATA[This Picture was created by
    Type-Style back in 2007. ]]>
            </b:about>
        </b:bio>
    </s>
    

    &lt;br /&gt; 在 XML 中没有任何特殊含义。另一方面,如果您打算生成 XHTML 作为输出,那么您所得到的只有在浏览器中才有意义,并且它应该显示为换行符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2013-09-07
      • 2017-03-16
      • 2011-02-27
      • 2014-01-07
      相关资源
      最近更新 更多