【问题标题】:Remove XML Node text using XSLT使用 XSLT 删除 XML 节点文本
【发布时间】:2015-08-30 16:54:00
【问题描述】:

我需要用 XSLT 修改 xml 文档。我需要的是从 xml 文档中删除一些节点名称。

例子:

<link ref="www.facebook.com">
       <c type="Hyperlink">www.facebook.com<c>
</link>

我需要把这个xml转换成如下(去掉&lt;c&gt;节点和属性形式xml),

<link ref="www.facebook.com">
       www.facebook.com
</link>

我尝试了多种方式来做到这一点,但都没有奏效。 有什么建议我该怎么做?

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    这是一种方法:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="c">
        <xsl:apply-templates/>
    </xsl:template>
    
    </xsl:stylesheet>
    

    这是另一个:

    <xsl:template match="link">
        <xsl:copy>
            <xsl:copy-of select="@* | c/text()"/>
        </xsl:copy>
    </xsl:template>
    

    【讨论】:

      【解决方案2】:

      还有一种方法:

      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
      <xsl:template match="link">
          <link>
          <xsl:copy-of select="@*"/><!-- copies the @ref attribute (and any other) -->    
          <xsl:value-of select="c[@type='hyperlink']"/>
          </link>    
      </xsl:template>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        • 1970-01-01
        • 2010-09-26
        • 2012-06-09
        • 2011-12-06
        相关资源
        最近更新 更多