tags:

views:

23

answers:

2

hi,

I got struck with a problem in VB.Net getting Xpath for an XML element as shown below:

<Parent xmlns:"http://www.sample.com"&gt;    
   <body>      
       <Child xmlns:"http://www.notsample.com"&gt;

           <element type="xyz"> ghghghghghg </element> 

       </Child>    
   </body>
</Parent>

I need Xpath of the "element" in above XML using VB.Net NameSpace Manager

For the "body" node i have done and its working but i couldnt do the same to "element":

dim bodynode as XMLNode=XML.SelectSingleNode(//ns:body,nsmngr)

where "nsmngr" is the namespacemanger created by me and "ns" is the name space of the "parent node" (which the "body" node inherits) added to the namespace manager as "ns"

Thanks Kiran

A: 

Given the following xml:

<OuterElem  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:test="http://www.w3.org/2001/XMLSchema-instance1" xmlns:test1="http://www.w3.org/2001/XMLSchema-instance1"&gt;
<test:parent>
    <test1:child1>blabla1</test1:child1>
    <test1:child2>blabla2</test1:child2>
    <test:child2>blabla3</test:child2>
</test:parent>
<test1:child1>blabla4</test1:child1>
</OuterElem>   

the following xslt (xslt 1.0) copies all nodes except "test:parent/test1:child1":

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:test="http://www.w3.org/2001/XMLSchema-instance1" xmlns:test1="http://www.w3.org/2001/XMLSchema-instance1"&gt;
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="test:parent/test1:child1"/>
</xsl:stylesheet>

The result will be:

<OuterElem xmlns:test="http://www.w3.org/2001/XMLSchema-instance1" xmlns:test1="http://www.w3.org/2001/XMLSchema-instance1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <test:parent>
        <test1:child2>blabla2</test1:child2>
        <test:child2>blabla3</test:child2>
    </test:parent>
    <test1:child1>blabla4</test1:child1>
</OuterElem>
Ando
This is not an XSLT question, therefore your answer is not what is wanted.
Dimitre Novatchev
@ dimitre: If you look at the examples you will see the 2 namespace bindings and the way to specify the xpath to a node belonging to 2 different namespaces: "test:parent/test1:child1" - so I would say it does answer the question
Ando
+1  A: 

There are two different ways to construct the needed XPath expression:

  1. Define a second namespace binding with NamespaceManager, say ns2: bound to http://www.notsample.com. Then use:

/*/ns:body/ns2:Child/ns2:element

  1. Don't use namespaces at all:

/*/*[name()='body']/*[name()='Child']/*[name()='element']

Dimitre Novatchev