使用 CDATA 和 XSL 时 XML 文件中的读取中断
Reading breaks in XML file when using CDATA and XSL
我试图在使用 CDATA
时读取换行符,但运气不佳。我做错了什么吗?我不断收到:
This Picture was created by <br /> Type-Style back in 2007.
作为输出,根本无法识别换行符m
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>
此答案假定您无法更改输入文档。使用字符串替换将 br
字符串转换为实际的换行符。使用 XSLT 1.0,进行字符串替换的标准方法是编写 递归模板 .
XSLT 样式表
命名模板无耻地从 Mads 的回答中盗用 here which in turn borrows from 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="'<br />'" />
<xsl:with-param name="with" select="' '"/>
</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。
源文档中的文字换行符将贯穿到输出中。尝试:
<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>
<br />
在XML中没有任何特殊含义。另一方面,如果您打算生成 XHTML 作为输出,那么您所得到的只有在浏览器中才有意义,并且它应该显示为换行符。
我试图在使用 CDATA
时读取换行符,但运气不佳。我做错了什么吗?我不断收到:
This Picture was created by <br /> Type-Style back in 2007.
作为输出,根本无法识别换行符m
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>
此答案假定您无法更改输入文档。使用字符串替换将 br
字符串转换为实际的换行符。使用 XSLT 1.0,进行字符串替换的标准方法是编写 递归模板 .
XSLT 样式表
命名模板无耻地从 Mads 的回答中盗用 here which in turn borrows from 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="'<br />'" />
<xsl:with-param name="with" select="' '"/>
</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。
源文档中的文字换行符将贯穿到输出中。尝试:
<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>
<br />
在XML中没有任何特殊含义。另一方面,如果您打算生成 XHTML 作为输出,那么您所得到的只有在浏览器中才有意义,并且它应该显示为换行符。