Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[XSLT] remove a particular node

 
Brian Grey
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a long time I haven't worked with XSLT/XPath. I'm trying to remove a particular node from an XML.

Here is an example:

<wap-provisioningdoc>
<characteristic type="PXLOGICAL">
<parm name="NAME" value="Prov_NAME"/>
<parm name="PROV-PIN" value="Prov_PIN"/>
<parm name="NAMEPAGE" value="Homepage_NAME"/>
<parm name="STARTPAGE" value="Homepage_URL"/>
</characteristic>
<characteristic type="NAPDEF">
<parm name="NAPID" value="NAP1"/>
<parm name="NAP-ADDRESS" value="Bearer_ACCESSPOINT"/>
</characteristic>
</wap-provisioningdoc>

I'd like to remove the node characteristic that has a type "NAPDEF".

the result should be:

<wap-provisioningdoc>
<characteristic type="PXLOGICAL">
<parm name="NAME" value="Prov_NAME"/>
<parm name="PROV-PIN" value="Prov_PIN"/>
<parm name="NAMEPAGE" value="Homepage_NAME"/>
<parm name="STARTPAGE" value="Homepage_URL"/>
</characteristic>
</wap-provisioningdoc>


So I wrote this:

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*[not(type='NAPDEF')]|node()"/>
</xsl:copy>
</xsl:template>
But it doesn't work. How can I select the nodes that have no characteristic node of type NAPDEF?

[ March 15, 2005: Message edited by: Brian Grey ]
[ March 15, 2005: Message edited by: Brian Grey ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

I find the exclusion pattern where things I don't want go into their own template much more readable than putting the stuff inline.
Hope that helps
[ March 16, 2005: Message edited by: Stephan H. Wissel ]
 
Brian Grey
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found my mistake. The command [not(@type='NAPDEF')] was in the wrong place.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl utput method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(@type='NAPDEF')]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic