我完全不熟悉XML/XSL/XSLT,虽然我一直在挖掘msdn,3schools.com和谷歌过去一小时,我不能弄清楚这一点。我认为是因为CDATA未通过xml解析,但我认为,因为我的编辑确实在节点上工作,所以我应该可以修复此问题...修复CDATA节的缩进
请注意,这不是一个非常重要的问题,我只是想多学习一点XSL,还有比修复看起来不像我想要的东西更好的方法。
因此...我的脚本将选项保存在XML文件中,我将在其中保存一些可能包含需要转义的字符的代码片段。一个小例子是:
<Snippet title="Version Test">
<![CDATA[
version := "AHK Version: " a_ahkversion
unicode := "& Supports Unicode: " (a_isunicode ? "Yes" : "No")
Msgbox % version "`n" unicode
]]>
</Snippet>
用下面的XSL,我收到了相当不错的压痕:
<!-- Extracted from: http://www.dpawson.co.uk/xsl/sect2/pretty.html (v2) -->
<!-- Cdata info: http://www.altova.com/forum/default.aspx?g=posts&t=1000002342 -->
<!-- Modified By RaptorX -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
indent="yes"
encoding="UTF-8"
cdata-section-elements="Snippet"/>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="comment()|processing-instruction()">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>
<!-- I have to keep the indentation here in this file as
i want it to be on the XML file -->
嗯,基本上它不匹配CDATA部分,所以周围的Googling我发现我可以做下面这有助于一点点,但产生这样的输出:
xsl:copy-of select="@*|node()" /> << -- by adding that i match cdata nodes too
Output:
<Snippet title="Version Test">
<![CDATA[
version := "AHK Version: " a_ahkversion
unicode := "Supports Unicode: " (a_isunicode ? "Yes" : "No")
Msgbox % version "`n" unicode
]]></Snippet> <<-- here is the problem I cant seem to put a newline there lol
所以,问题是:
我如何告诉XSL缩进CDATA部分,因为它与一切做:
<root>
<child/>
</root>
<Snippet title="Version Test">
<![CDATA[
version := "AHK Version: " a_ahkversion
unicode := "Supports Unicode: " (a_isunicode ? "Yes" : "No")
Msgbox % version "`n" unicode
]]> << --- this is what im looking for
</Snippet>
为什么重要的是文本要缩进?您要求序列化程序更改文档的语义......或者说输出序列化程序无法知道,在此特殊实例中,将空白插入到文本节点的中间不会更改语义。假设CDATA部分包含一些Python代码......在每行的开头插入额外的空格几乎肯定会改变语义。 – 2011-03-14 21:33:28
好问题,+1。查看我的答案,获取完整且经过测试的解决方案,可真正缩进原始CDATA部分中的文本行。还提供了广泛的解释。 – 2011-03-15 03:01:03