我在尝试使用CDATA
时读取换行符,但运气不佳。有什么我做错了吗?我一直在说:
This Picture was created by <br /> Type-Style back in 2007.
作为输出,在allm处无法识别换行符
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>
答案 0 :(得分:1)
此答案假定您无法更改输入文档。使用字符串替换将br
字符串转换为实际换行符。使用XSLT 1.0,执行字符串替换的标准方法是编写递归模板。
XSLT样式表
从Mads的回答here中无耻地窃取了指定的模板,而Dave Pawson又从online here借用。
<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>
答案 1 :(得分:1)
源文档中的文字换行符将继续输出到输出。尝试:
<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作为输出产生,那么你所拥有的只在浏览器中有意义,并且它应该显示为换行符。