XSLT Remove Element Based On Attribute

2019-08-16 02:41发布

问题:

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>

回答1:

Your XML source has attribute name "URL" but you are trying to match "Url".



回答2:

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 xslt