Dur*_*rkD 3 xml xslt whitespace
我有许多应遵循以下格式的 xml 文件:
Run Code Online (Sandbox Code Playgroud)<root> <question>What is the answer?</question> <answer choice="A">Some</answer> <answer choice="B">Answer</answer> <answer choice="C">Text</answer> </root>
但它来自带有注释的网络界面(我无法控制输出),最终看起来像这样:
Run Code Online (Sandbox Code Playgroud)<root> <question>What is the answer?</question> <answer choice="A"><!--some comment --> Some </answer choice="B"> <answer> <!--some comment --> Answer </answer> <answer choice="C"><!--another comment --> Text</answer> </root>
删除注释后的输出结果如下:
Run Code Online (Sandbox Code Playgroud)What is the answer? A\t Some B\t Answer C\t Text
现在,我设置了一个 xsl 表来使用以下命令删除注释:
<xsl:template match="comment()"/>
以及其他一些身份模板应用程序。
我会使用 normalize-space(),但它会从答案文本中删除我确实想要的换行符。我正在寻找的是一种仅删除“空白”或前面和结尾的“额外”换行符的方法。有没有好的方法可以做到这一点?
另请注意:最终输出是 Adobe Indesign,它使用 XSLT 1.0。
[编辑 - XSL 如下]。
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:strip-space elements="*" /> <xsl:template match = "@*|node()|processing-instruction()" name="identity"> <xsl:copy> <xsl:apply-templates select="@*|node()|processing-instruction()"/> </xsl:copy> </xsl:template> <xsl:template match="comment()"/> <xsl:template match="//answer"><xsl:value-of select="@choice"/> <xsl:text>	</xsl:text><xsl:call-template name="identity"/> </xsl:template> <xsl:template match="//question"> <xsl:text>00	</xsl:text><xsl:call-template name="identity"/> </xsl:template> </xsl:stylesheet>
小智 5
这个样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="comment()"/>
<xsl:template match="@choice">
<xsl:value-of select="concat(.,'	')"/>
</xsl:template>
<xsl:template match="question|answer">
<xsl:call-template name="identity"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出:
<root><question>What is the answer?</question>
<answer>A Some</answer>
<answer>B Answer</answer>
<answer>C Text</answer>
</root>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7055 次 |
| 最近记录: |