使用 xslt 删除节点 xml

Delete node xml by using xslt

如果电话中的状态不等于'A'

,我想删除有条件的节点

这是我的xml

<name>
    <name>
        <firstName>Yuio</firstName>
        <lastName>Kuyoshitu</lastName>
        <telephoneNav>
            <detail>
                <action>A</action>
                <number>1745</number>
            </detail>
            <detail>
                <action>P</action>
                <number>1189</number>
            </detail>
        </telephoneNav>
    </name>
    <name>
        <firstName>Huio</firstName>
        <lastName>Kuyoshitu</lastName>
        <telephoneNav>
            <detail>
                <action>P</action>
                <number>0902</number>
            </detail>
            <detail>
                <action>P</action>
                <number>0901</number>
            </detail>
        </telephoneNav>
    </name>
</name>

如果name node没有A状态的电话号码。我想删除名称节点

这是预期的结果

<?xml version="1.0" encoding="utf-16"?><name>
<name>
    <firstName>Yuio</firstName>
    <lastName>Kuyoshitu</lastName>
    <telephoneNav>
       <detail>
            <action>A</action>
            <number>1745</number>
        </detail>
    </telephoneNav>
</name>

我试试这个代码。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/name/name/telephoneNav/detail[not(action = 'A')]"/>
  <xsl:template match="/name/name/telephoneNav/text()[not(normalize-space())]"/>
</xsl:stylesheet>

但我仍然得到空的名称节点telephoneNav

这是我的结果

<?xml version="1.0" encoding="utf-16"?><name>
    <name>
        <firstName>Yuio</firstName>
        <lastName>Kuyoshitu</lastName>
        <telephoneNav>
           <detail>
                <action>A</action>
                <number>1745</number>
            </detail>
        </telephoneNav>
    </name>
    <name>
        <firstName>Huio</firstName>
        <lastName>Kuyoshitu</lastName>
        <telephoneNav />
    </name>
</name>

If name node does not have telephone number with A status. I want to delete name node

为此,您可以这样做:

<xsl:template match="/name/name[not(telephoneNav/detail/action = 'A')]"/>

但您似乎还想删除没有 A 操作的 detail 个节点。这可以使用:

<xsl:template match="detail[not(action = 'A')]"/>