简体   繁体   中英

XSLT Remove Element Based On Attribute

I am struggling with some basic XSLT. I would like to remove an element from some XML depending on whether it has a certain attribute.

The XML looks like so:

<root>
    <Request URL="www.google.com">
        <id name="google"/>
    </Request>
    <Request URL="www.yahoo.com">
        <id name="yahoo"/>
    </Request>
</root>

I would like to remove the Request element if the URL is "www.google.com" and also to remove the element and the , so I would end up with the following:

<root>
    <Request URL="www.yahoo.com">
        <id name="yahoo"/>
    </Request>
</root>

I have the following so far, but it isn't working:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!--identity template copies everything forward by default-->     
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!--empty template suppresses this attribute-->
  <xsl:template match="Request[@Url='www.google.com']"/>
</xsl:stylesheet>

Just a tip: xml is case sensitive. In input xml you have attribute URL in Request element. But in xslt you have @Url. So try make this

<xsl:template match="Request[@URL='www.google.com'] "/>

您的XML源具有属性名称“ URL”,但是您尝试匹配“ Url”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM