473,221 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

XPath problem with two xmlns

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.

Nov 12 '05 #1
4 8930


Gismo wrote:
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?


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/

Nov 12 '05 #2

Uzytkownik "Martin Honnen" <ma*******@yahoo.de> napisal w wiadomosci
news:Oq*************@TK2MSFTNGP09.phx.gbl...


Gismo wrote:
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?
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/reportd
esigner" 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/

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.

Nov 12 '05 #3


Gismo wrote:
Uzytkownik "Martin Honnen" <ma*******@yahoo.de> napisal w wiadomosci
news:Oq*************@TK2MSFTNGP09.phx.gbl...

Gismo wrote:

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?
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/reportd
esigner"
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'"
)

There is no method like setProperty.


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/

Nov 12 '05 #4

Thanx, it works fine now.
:-)

Gismo.

Uzytkownik "Martin Honnen" <ma*******@yahoo.de> napisal w wiadomosci
news:#T**************@TK2MSFTNGP10.phx.gbl...


Gismo wrote:
Uzytkownik "Martin Honnen" <ma*******@yahoo.de> napisal w wiadomosci
news:Oq*************@TK2MSFTNGP09.phx.gbl...

Gismo wrote:
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?

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/reportd esigner"
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'" )

There is no method like setProperty.


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/

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hollywood | last post by:
I have the following XSD created in VS.NET 2003: <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="ReferralSchama" targetNamespace="http://test.org/Referral"...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
2
by: Monty | last post by:
Despite reading posts in Google, I don't understand XPATH. Can someone help me write an XPATH. From Google I think my problem is that the default namespace does not have a prefix. I can't change...
6
by: J.Marsch | last post by:
I must be completely losing my mind. I have some code that writes to config files. It works great with app.config files, but fails miserably with web.config files. For the life of me, I cannot...
2
by: A. W. Dunstan | last post by:
I'm trying to figure out how XPath expressions work, and how I can use them to extract data into a particular format. I can extract the data I want using an XPath expression, but not with an XSLT...
1
by: reyesvsn | last post by:
Hello everybody, I have a question concerning XPath expressions and namespaces. Consider this XML: <?xml version="1.0" encoding="UTF-8"?> <newsMessage ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.