【问题标题】:XML Removing child node keeping value using xsltXML使用xslt删除子节点保留值
【发布时间】:2018-06-18 07:53:42
【问题描述】:

我正在尝试删除子节点并保留父节点和子节点值,就像这样。我的 xml 看起来像

<parent>
<child>
<value>
123
</value>
</child>
</parent>

输出看起来像

<parent>123</parent>

我需要使用任何 xslt 进行解析。任何帮助将不胜感激。

【问题讨论】:

  • 您应该编辑您的问题以显示您尝试过的任何 XSLT。谢谢!

标签: xml xslt


【解决方案1】:

如果您想删除一个元素及其所有后代,您可以这样做...

<xsl:template match="child" />

但是,如果您只是想删除该元素,但保留其后代,您可以这样做...

<xsl:template match="child">
    <xsl:apply-templates />
</xsl:template>

其中&lt;xsl:apply-templates /&gt;&lt;xsl:apply-templates select="node()" /&gt; 的缩写

试试这个 XSLT

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

    <xsl:template match="child|value">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    试试这个:

    <xsl:template match="parent">
        <xsl:copy>
            <xsl:value-of select="."/>
        </xsl:copy>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 2015-08-30
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多