all groups > dotnet xml > november 2006 >
You're in the

dotnet xml

group:

XPath selecting on subnodes


XPath selecting on subnodes Hapsdog
11/23/2006 7:55:23 AM
dotnet xml: Hi,

I have the following XML:

<a>
<b>
<c name="bob"/>
<c name="billy"/>
</b>
<b>
<c name="george"/>
<c name="nick"/>
</b>
</a>

I'm trying to write an XPath-expression to get this XML:

<b>
<c name="billy"/>
</b>
<b>
<c name="george"/>
<c name="nick"/>
</b>

but I can't get it right.

I have tried /a/b/c[@name != 'bob']/.. but it does not work. I know
why, but I don't know how to fix it.

Any help is appreciated.

/Bent
Re: XPath selecting on subnodes Oleg Tkachenko [MVP]
11/23/2006 6:18:57 PM
[quoted text, click to view]

You can't. XPath is selection language and what you need is filtering.
Once you select node b you get it as is with all children nodes.
A simple XSLT script could do the job:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Filter this one out -->
<xsl:template match="c[@name='bob']"/>
</xsl:stylesheet>

--
Oleg Tkachenko [XML MVP, MCPD]
AddThis Social Bookmark Button