XSL完全删除注释(包括空格)

发布于 2024-10-07 15:30:29 字数 1737 浏览 1 评论 0原文

我有许多应遵循以下格式的 xml 文件:

<前><代码><根>; <问题>答案是什么? <答案选择=“A”>一些 <答案选择=“B”>答案 <答案选择=“C”>文本

但它来自带有注释的网络界面(我无法控制输出),最终看起来像这样:

<前><代码><根>; <问题>答案是什么? <答案选择=“A”><!--一些评论 --> 一些 <答案> 回答 <答案选择=“C”><!--另一条评论 --> 文字

删除注释后的输出结果如下:

答案是什么?
在


一些
乙\t
回答
C\t     
文本

现在,我设置了一个 xsl 表来使用以下命令删除注释:

和一些其他身份模板应用程序。

我会使用 normalize-space(),但它会从答案文本中删除我确实想要的换行符。我正在寻找的是一种仅删除“空白”或前面和结尾的“额外”换行符的方法。有没有好的方法可以做到这一点?

另请注意:最终输出是 Adob​​e Indesign,它使用 XSLT 1.0。

[编辑 - XSL 如下]。















&#09;
 


00&#09;



I have a number of xml files that should follow this format:

<root>
<question>What is the answer?</question>
<answer choice="A">Some</answer>
<answer choice="B">Answer</answer>
<answer choice="C">Text</answer>
</root>

But it comes in from a web interface (I cannot control the output) with Comments and ends up looking like this:

<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>

The output - after removing the comments ends up like this:

What is the answer?
A\t


Some
B\t
Answer
C\t     
Text

Now, I have an xsl sheet set up to strip out comments using:

<xsl:template match="comment()"/>

and some other Identity template applications.

I would use normalize-space(), but it removes the newlines that I do want from the answer text. What I am looking for is a way to remove only "blank" or preceding and ending "extra" newlines. Is there a good way to do this?

Also note: The final output is Adobe Indesign, which uses XSLT 1.0.

[Edit - the XSL is below].

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-10-14 15:30:29

该样式表:

<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>

输出:

<root><question>What is the answer?</question>
<answer>A   Some</answer>
<answer>B   Answer</answer>
<answer>C   Text</answer>
</root>

This stylesheet:

<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>

Output:

<root><question>What is the answer?</question>
<answer>A   Some</answer>
<answer>B   Answer</answer>
<answer>C   Text</answer>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文