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()用于确保输入数据没有不需要的空格.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念