XSL 彻底删除注释(包括空格) [英] XSL to completely remove comments (Including the space)

查看:19
本文介绍了XSL 彻底删除注释(包括空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些应该遵循这种格式的 xml 文件:

<块引用>

<问题>答案是什么?</question><answer choice="A">一些</answer><answer choice="B">答案</answer><answer choice="C">文本</answer></root>

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

<块引用>

<问题>答案是什么?</question><answer choice="A"><!--一些评论-->一些</answer selection="B"><回答><!--一些评论-->回答</回答><answer choice="C"><!--另一条评论-->文本</root>

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

<块引用>

答案是什么?在一些乙\t回答C\t文本

现在,我设置了一个 xsl 表来删除注释:

<块引用>

以及其他一些身份模板应用程序.

我会使用 normalize-space(),但它会从答案文本中删除我想要的换行符.我正在寻找的是一种仅删除空白"或前后额外"换行符的方法.有什么好的办法吗?

另请注意:最终输出是 Adob​​e Indesign,它使用 XSLT 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:模板><xsl:template match="comment()"/><xsl:template match="@choice"><xsl:value-of select="concat(.,'&#x9;')"/></xsl:模板><xsl:template match="问题|答案"><xsl:call-template name="identity"/><xsl:text>&#xA;</xsl:text></xsl:模板></xsl:stylesheet>

输出:

答案是什么?<answer>A 一些</answer><answer>B 答案</answer><answer>C 文本</answer></root>

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>&#09;</xsl:text><xsl:call-template name="identity"/>
</xsl:template> 

<xsl:template match="//question">
<xsl:text>00&#09;</xsl:text><xsl:call-template name="identity"/>
</xsl:template>

</xsl:stylesheet>

解决方案

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(.,'&#x9;')"/>
    </xsl:template>
    <xsl:template match="question|answer">
        <xsl:call-template name="identity"/>
        <xsl:text>&#xA;</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>

这篇关于XSL 彻底删除注释(包括空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆