【问题标题】:how to remove the xml elements with the help of the xsl?如何在 xsl 的帮助下删除 xml 元素?
【发布时间】:2017-01-22 13:40:17
【问题描述】:

如何借助xsl去除xml中的子元素、节点和父元素。

这是我的 xml 代码。

<?xml version="1.0" encoding="UTF-8"?>
<Checkpax xmlns="http://xml.api.com/test">
    <customerLevel>
        <surname>MUKHERJEE</surname>
        <type>A</type>
        <gender>M</gender>
        <otherPaxDetails>
            <givenName>JOY</givenName>
            <title>MR</title>
            <age>11</age>
        </otherPaxDetails>
        <otherPaxDetails>
            <title>MR</title>
        </otherPaxDetails>
        <staffDetails>
            <staffInfo/>
            <staffCategoryInfo>
                <attributeDetails>
                    <attributeType>NA</attributeType>
                </attributeDetails>
            </staffCategoryInfo>
        </staffDetails>
        <productLevel>
            <legLevel>
                <legLevelIndicator>
                    <statusDetails>
                        <indicator>abc</indicator>
                        <action>1</action>
                    </statusDetails>
                </legLevelIndicator>
            </legLevel>
        </productLevel>
        <CustomerLevel>
            <legLevel>
                <legLevelIndicator>
                    <statusDetails>
                        <indicator>cde</indicator>
                        <action>1</action>
                    </statusDetails>
                </legLevelIndicator>
            </legLevel>
        </CustomerLevel>
    </customerLevel>
</Checkpax>

预期的输出 xml:

<Checkpax xmlns="http://xml.api.com/test">
    <paxDetails>
        <surname>MUKHERJEE</surname>
        <gender>M</gender>
    </paxDetails>
    <otherPaxDetails>
        <title>MR</title>
        <age>11</age>
    </otherPaxDetails>
    <otherPaxDetails>
        <title>MR</title>
    </otherPaxDetails>
    <staffDetails>
        <staffCategoryInfo>
            <attributeDetails>
                <attributeType>NA</attributeType>
            </attributeDetails>
        </staffCategoryInfo>
    </staffDetails>
    <legLevelIndicator>
        <statusDetails>
            <indicator>abc</indicator>
        </statusDetails>
    </legLevelIndicator>
    <CustomerLevel>
        <legLevel>
            <legLevelIndicator>
                <indicator>cde</indicator>
                <action>1</action>
            </legLevelIndicator>
        </legLevel>
    </CustomerLevel>
</Checkpax>

我尝试过的 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://xml.api.com/test"
    xmlns:ns0="http://xml.api.com/test"
    exclude-result-prefixes="ns0">
    <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>

  <!-- Apply all child nodes; don't copy the element itself -->
  <xsl:template match="ns0:customerLevel| ns0:customerDetails| ns0:paxDetails">
    <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>

请建议使用 xsl 样式表来获得预期的 xml 输出。我不知道如何删除元素。这对于许多正在寻找使用 xsl 在 xml 中删除元素的人来说将很有帮助。

【问题讨论】:

    标签: xml xslt stylesheet


    【解决方案1】:

    AFAICT,您的第二个模板必须是:

    <xsl:template match="ns0:productLevel | ns0:legLevel">
        <xsl:apply-templates/>
    </xsl:template>
    

    这将删除 productLevellegLevel 包装器,并将 legLevelIndicator 提升为 staffDetails 的同级。


    给我一​​些建议如何删除下面的&lt;age&gt;11&lt;/age&gt;标签 其他PaxDetails

    添加一个与age匹配的empty模板:

    <xsl:template match="ns0:age"/>
    

    如果您可能有其他同名元素,则缩小匹配模式:

    <xsl:template match="ns0:otherPaxDetails/ns0:age"/>
    

    【讨论】:

    • 给我一些建议如何删除 otherPaxDetails 元素下的 11 标签。@michael.hor257k
    • 是的,这很有用。为我工作。确保我会标记它。@michael.hor257k
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多