微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – 如何仅使用XSLT去除回车?

我有一个xml代码,可以有两种形式:

表格1

<?xml version="1.0">
<info>
</info>

表格2

<?xml version="1.0">
<info>
  <a href="http://server.com/foo">bar</a>
  <a href="http://server.com/foo">bar</a>
</info>

从循环中我读取每种形式的xml并将其传递给xslt样式表.

XSLT代码

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*" />

    <xsl:template match="*|@*|text()">
       <xsl:apply-templates select="/info/a"/>
    </xsl:template> 

    <xsl:template match="a">
       <xsl:value-of select="concat(text(),' ',@href)"/>
       <xsl:text>&#13;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

我得到了这个:


bar http://server.com/foo
bar http://server.com/foo

如何只使用XSLT删除一个空行?

From a loop I read each form of xml and pass it to an xslt stylesheet.

可能来自您的应用程序在空表单(表单1)上执行样式表会导致此问题.尝试仅通过执行样式表来处理此问题,无论表单是否为空.

此外,您可能希望将样式表更改为以下样式表:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0">

    <xsl:output method="text"/>
    <xsl:strip-space elements="*" />

    <xsl:template match="info/a">
        <xsl:value-of select="concat(normalize-space(.),normalize-space(@href))"/>
            <xsl:if test="follwing-sibling::a">
             <xsl:text>&#xA;</xsl:text>
            </xsl:if>
    </xsl:template>

</xsl:stylesheet>

其中normalize-space()用于确保输入数据没有不需要的空格.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。