Connecting Tech Pros Worldwide Forums | Help | Site Map

XPath problem with two xmlns

Gismo
Guest
 
Posts: n/a
#1: Nov 12 '05
I have got file raport.rld which is an XML file generated by MS Reporting
Services.

The problem is:

in this file are tags from two different namespaces

xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini
tion"
xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"



when I'm trying to use XPATH to get <teble> tag it doesn't work. (the
result is empty)

I've removed All tegs like

<rd:TypeName>System.Int32</rd:TypeName>

Now all is working fine but what to do when I have tags from two different
namespaces?

I don't want to remove all tags from second namespace.



Tom.




Martin Honnen
Guest
 
Posts: n/a
#2: Nov 12 '05

re: XPath problem with two xmlns




Gismo wrote:
[color=blue]
> I have got file raport.rld which is an XML file generated by MS Reporting
> Services.
>
> The problem is:
>
> in this file are tags from two different namespaces
>
> xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini
> tion"
> xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"
>
>
>
> when I'm trying to use XPATH to get <teble> tag it doesn't work. (the
> result is empty)
>
> I've removed All tegs like
>
> <rd:TypeName>System.Int32</rd:TypeName>
>
> Now all is working fine but what to do when I have tags from two different
> namespaces?[/color]

You need to declare a prefix for every namespace and then use the prefix
in your XPath expression. How you do that depends on the environment, if
you use XPath inside of an XSLT stylesheet you can simply declare the
prefixes with
<xsl:stylesheet

xmlns:rd="xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"
and then use
rd:TypeName
in your XPath expression.

If you are using an API like selectNodes/selectSingleNode then you need
to call
xmlDocument.setProperty("SelectionNamespaces",

"xmlns:rd='http://schemas.microsoft.com/SQLServer/reporting/reportdesigner'")

--

Martin Honnen
http://JavaScript.FAQTs.com/

Gismo
Guest
 
Posts: n/a
#3: Nov 12 '05

re: XPath problem with two xmlns



Uzytkownik "Martin Honnen" <mahotrash@yahoo.de> napisal w wiadomosci
news:OqLc8L$FEHA.692@TK2MSFTNGP09.phx.gbl...[color=blue]
>
>
> Gismo wrote:
>[color=green]
> > I have got file raport.rld which is an XML file generated by MS[/color][/color]
Reporting[color=blue][color=green]
> > Services.
> >
> > The problem is:
> >
> > in this file are tags from two different namespaces
> >
> >[/color][/color]
xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini[color=blue][color=green]
> > tion"
> >[/color][/color]
xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"[color=blue][color=green]
> >
> >
> >
> > when I'm trying to use XPATH to get <teble> tag it doesn't work. (the
> > result is empty)
> >
> > I've removed All tegs like
> >
> > <rd:TypeName>System.Int32</rd:TypeName>
> >
> > Now all is working fine but what to do when I have tags from two[/color][/color]
different[color=blue][color=green]
> > namespaces?[/color]
>
> You need to declare a prefix for every namespace and then use the prefix
> in your XPath expression. How you do that depends on the environment, if
> you use XPath inside of an XSLT stylesheet you can simply declare the
> prefixes with
> <xsl:stylesheet
>
>[/color]
xmlns:rd="xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportd
esigner"[color=blue]
> and then use
> rd:TypeName
> in your XPath expression.
>
> If you are using an API like selectNodes/selectSingleNode then you need
> to call
> xmlDocument.setProperty("SelectionNamespaces",
>
>[/color]
"xmlns:rd='http://schemas.microsoft.com/SQLServer/reporting/reportdesigner'"
)[color=blue]
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/
>[/color]
There is no method like setProperty.
My example code is:

My example code is:
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("c:/tab2.xml");

XmlElement root = doc.DocumentElement;

XmlNodeList nodeList;
nodeList = root.SelectNodes("//Table");
if (nodeList != null)
foreach (XmlNode s1 in nodeList)
{
try
{
Console.WriteLine(s1.Name);
}
catch (XmlException e) {
Console.WriteLine(e.Message);
}
}
}
}




Gismo.



Martin Honnen
Guest
 
Posts: n/a
#4: Nov 12 '05

re: XPath problem with two xmlns




Gismo wrote:
[color=blue]
> Uzytkownik "Martin Honnen" <mahotrash@yahoo.de> napisal w wiadomosci
> news:OqLc8L$FEHA.692@TK2MSFTNGP09.phx.gbl...
>[color=green]
>>
>>Gismo wrote:
>>
>>[color=darkred]
>>>I have got file raport.rld which is an XML file generated by MS[/color][/color]
>
> Reporting
>[color=green][color=darkred]
>>>Services.
>>>
>>>The problem is:
>>>
>>>in this file are tags from two different namespaces
>>>
>>>[/color][/color]
>
> xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini
>[color=green][color=darkred]
>>>tion"
>>>[/color][/color]
>
> xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"
>[color=green][color=darkred]
>>>
>>>
>>> when I'm trying to use XPATH to get <teble> tag it doesn't work. (the
>>>result is empty)
>>>
>>> I've removed All tegs like
>>>
>>><rd:TypeName>System.Int32</rd:TypeName>
>>>
>>>Now all is working fine but what to do when I have tags from two[/color][/color]
>
> different
>[color=green][color=darkred]
>>>namespaces?[/color]
>>
>>You need to declare a prefix for every namespace and then use the prefix
>>in your XPath expression. How you do that depends on the environment, if
>>you use XPath inside of an XSLT stylesheet you can simply declare the
>>prefixes with
>> <xsl:stylesheet
>>
>>[/color]
>
> xmlns:rd="xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportd
> esigner"
>[color=green]
>>and then use
>> rd:TypeName
>>in your XPath expression.
>>
>>If you are using an API like selectNodes/selectSingleNode then you need
>>to call
>> xmlDocument.setProperty("SelectionNamespaces",
>>
>>[/color]
>
> "xmlns:rd='http://schemas.microsoft.com/SQLServer/reporting/reportdesigner'"
> )
>[/color]
[color=blue]
> There is no method like setProperty.[/color]

Sorry, I thought you might be using MSXML but you use .NET, there indeed
is no such method.

With .NET you need an XmlNamespaceManager and declare the prefixes for
your XPath expressions and then pass that manager to the SelectNodes method.
For instance with the example document being

<?xml version="1.0" encoding="UTF-8"?>
<gods xmlns="http://vatican.va/2004/gods">
<god name="Kibo" />
<god name="Jaffo" />
<dv:devil xmlns:dv="http://vatican.va/2004/devils" name="Xibo" />
</gods>

the following successfully queries the document using XPath

using System;
using System.Xml;

public class Test20040402 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040402.xml");
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xmlDocument.NameTable);
namespaceManager.AddNamespace("gs", "http://vatican.va/2004/gods");
namespaceManager.AddNamespace("dv", "http://vatican.va/2004/devils");
XmlNodeList gods = xmlDocument.SelectNodes("/gs:gods/gs:god",
namespaceManager);
Console.WriteLine("Found {0} elements.", gods.Count);
foreach (XmlNode node in gods) {
Console.WriteLine(node.Name);
}
gods = xmlDocument.SelectNodes("/gs:gods/dv:devil", namespaceManager);
Console.WriteLine("Found {0} elements.", gods.Count);
foreach (XmlNode node in gods) {
Console.WriteLine(node.Name);
}
}

}
--

Martin Honnen
http://JavaScript.FAQTs.com/

Gismo
Guest
 
Posts: n/a
#5: Nov 12 '05

re: XPath problem with two xmlns



Thanx, it works fine now.
:-)

Gismo.



Uzytkownik "Martin Honnen" <mahotrash@yahoo.de> napisal w wiadomosci
news:#T5Ir9JGEHA.1240@TK2MSFTNGP10.phx.gbl...[color=blue]
>
>
> Gismo wrote:
>[color=green]
> > Uzytkownik "Martin Honnen" <mahotrash@yahoo.de> napisal w wiadomosci
> > news:OqLc8L$FEHA.692@TK2MSFTNGP09.phx.gbl...
> >[color=darkred]
> >>
> >>Gismo wrote:
> >>
> >>
> >>>I have got file raport.rld which is an XML file generated by MS[/color]
> >
> > Reporting
> >[color=darkred]
> >>>Services.
> >>>
> >>>The problem is:
> >>>
> >>>in this file are tags from two different namespaces
> >>>
> >>>[/color]
> >
> >[/color][/color]
xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini[color=blue][color=green]
> >[color=darkred]
> >>>tion"
> >>>[/color]
> >
> >[/color][/color]
xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"[color=blue][color=green]
> >[color=darkred]
> >>>
> >>>
> >>> when I'm trying to use XPATH to get <teble> tag it doesn't work. (the
> >>>result is empty)
> >>>
> >>> I've removed All tegs like
> >>>
> >>><rd:TypeName>System.Int32</rd:TypeName>
> >>>
> >>>Now all is working fine but what to do when I have tags from two[/color]
> >
> > different
> >[color=darkred]
> >>>namespaces?
> >>
> >>You need to declare a prefix for every namespace and then use the prefix
> >>in your XPath expression. How you do that depends on the environment, if
> >>you use XPath inside of an XSLT stylesheet you can simply declare the
> >>prefixes with
> >> <xsl:stylesheet
> >>
> >>[/color]
> >
> >[/color][/color]
xmlns:rd="xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportd[color=blue][color=green]
> > esigner"
> >[color=darkred]
> >>and then use
> >> rd:TypeName
> >>in your XPath expression.
> >>
> >>If you are using an API like selectNodes/selectSingleNode then you need
> >>to call
> >> xmlDocument.setProperty("SelectionNamespaces",
> >>
> >>[/color]
> >
> >[/color][/color]
"xmlns:rd='http://schemas.microsoft.com/SQLServer/reporting/reportdesigner'"[color=blue][color=green]
> > )
> >[/color]
>[color=green]
> > There is no method like setProperty.[/color]
>
> Sorry, I thought you might be using MSXML but you use .NET, there indeed
> is no such method.
>
> With .NET you need an XmlNamespaceManager and declare the prefixes for
> your XPath expressions and then pass that manager to the SelectNodes[/color]
method.[color=blue]
> For instance with the example document being
>
> <?xml version="1.0" encoding="UTF-8"?>
> <gods xmlns="http://vatican.va/2004/gods">
> <god name="Kibo" />
> <god name="Jaffo" />
> <dv:devil xmlns:dv="http://vatican.va/2004/devils" name="Xibo" />
> </gods>
>
> the following successfully queries the document using XPath
>
> using System;
> using System.Xml;
>
> public class Test20040402 {
> public static void Main (string[] args) {
> XmlDocument xmlDocument = new XmlDocument();
> xmlDocument.Load(@"test20040402.xml");
> XmlNamespaceManager namespaceManager = new
> XmlNamespaceManager(xmlDocument.NameTable);
> namespaceManager.AddNamespace("gs", "http://vatican.va/2004/gods");
> namespaceManager.AddNamespace("dv", "http://vatican.va/2004/devils");
> XmlNodeList gods = xmlDocument.SelectNodes("/gs:gods/gs:god",
> namespaceManager);
> Console.WriteLine("Found {0} elements.", gods.Count);
> foreach (XmlNode node in gods) {
> Console.WriteLine(node.Name);
> }
> gods = xmlDocument.SelectNodes("/gs:gods/dv:devil",[/color]
namespaceManager);[color=blue]
> Console.WriteLine("Found {0} elements.", gods.Count);
> foreach (XmlNode node in gods) {
> Console.WriteLine(node.Name);
> }
> }
>
> }
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/
>[/color]


Closed Thread