XSLT Remove Element Based On Attribute

自作多情 提交于 2019-12-13 04:35:05

问题


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'] "/>


来源:https://stackoverflow.com/questions/18618294/xslt-remove-element-based-on-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!