加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:34:01 所属栏目:百科 来源:网络整理
导读:我有一个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:st
我有一个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()用于确保输入数据没有不需要的空格.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读