I have the following XML code.
You will notice the tag Description is repeated, but with different attributes.
I am using XSLT to try and remove the Description tag with the enabled attribute.
<Batch>
- <Promotion>
<LastUpdated>2008-01-22T11:58:05+00:00</LastUpdated>
<MajorVersion>1</MajorVersion>
<MinorVersion>29</MinorVersion>
<PromotionID>000873</PromotionID>
<Description enabled="1">*P* Free Mistletoe</Description>
<Description country="GB" language="en" variant="">WANTED LINE 1</Description>
</Promotion>
<Promotion>
<LastUpdated>2008-01-22T11:58:05+00:00</LastUpdated>
<MajorVersion>1</MajorVersion>
<MinorVersion>29</MinorVersion>
<PromotionID>000874</PromotionID>
<Description enabled="1">*P* Free Mistletoe</Description>
<Description country="GB" language="en" variant="">WANTED LINE 2</Description>
</Promotion>
</batch>
This is what I am trying to get to, there are other tags, it is the removal of one
line based on an attribute I am trying to resolve.
- <promotions>
- <promotion>
<promotionID>000873</promotionID>
<description country="GB" language="en" variant="">WANTED LINE 1</description>
</promotion>
- <promotion>
<promotionID>000874</promotionID>
<description country="GB" language="en" variant="">WANTED LINE 2</description>
</promotion>
</promotions>
The XSLT code I am using is
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//promotion/Description[@country='GB']"/>
<xsl:template match="/">
<promotions>
<xsl:for-each select="Batch/Promotion">
<promotion>
<promotion_id><xsl:value-of select="PromotionID"/></promotion_id>
<description><xsl:value-of select="Description"/></description>
</promotion>
</xsl:for-each>
</promotions>
</xsl:template>
</xsl:stylesheet>
If anyone could point me in the right direction I would be very grateful.
Paul
Generally, in order to remove an element, you have to specify a template without any contents. In your case, this could be:
In your XSLT code, however, you have the somewhat special case of constructing your own
<description>element. In order to get exactly the value of the desired<Description>element, select it in your<xsl:value-of>element:This is what you have described in your question, however your expected result code implies that you already want to copy the attributes of the
<Description>element? In that case, I’d go for this solution with<xsl:copy-of>:It copies the whole contents of the
<Description>element (node()), as well as any of its attributes (@*).