By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

xpath query with namespace

P: n/a
dc
i have a xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<validate xmlns="http://tempuri.org/fieldValidate.xsd">
<field name="Short Name" type="SN" length="10">
<requiredChar value="Y" errMsg="required Y" />
<requiredChar value="X" errMsg="required X" />
<bannedChar value="Z" errMsg="banned Z" />
<bannedChar value="A" errMsg="banned A" />
</field>
<field name="Name" type="N" length="250">
<requiredChar value="Y" errMsg="required Y" />
<requiredChar value="X" errMsg="required X" />
<bannedChar value="Z" errMsg="banned Z" />
<bannedChar value="A" errMsg="banned A" />
</field>
</validate>

And I want to get XmlElement of field which field type is SN. Then I used
this.xmlDoc.SelectSingleNode("//field[@type='SN']" );
But it fail to get the field element. Then I try to delete the
xmlns="http://tempuri.org/fieldValidate.xsd" in validate field, the code
works. After reading some doc, it said:
There is no namespace support for XPath queries specified directly in the
URL. If you want to use a namespace in an XPath query, template should be
used.
I am new in XML, I hope I havn't lead you to a wrong direction

Thanks!
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
http://www.topxml.com/people/bosley/defaultns.asp
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"dc" <a@a.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
i have a xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<validate xmlns="http://tempuri.org/fieldValidate.xsd">
<field name="Short Name" type="SN" length="10">
<requiredChar value="Y" errMsg="required Y" />
<requiredChar value="X" errMsg="required X" />
<bannedChar value="Z" errMsg="banned Z" />
<bannedChar value="A" errMsg="banned A" />
</field>
<field name="Name" type="N" length="250">
<requiredChar value="Y" errMsg="required Y" />
<requiredChar value="X" errMsg="required X" />
<bannedChar value="Z" errMsg="banned Z" />
<bannedChar value="A" errMsg="banned A" />
</field>
</validate>

And I want to get XmlElement of field which field type is SN. Then I used
this.xmlDoc.SelectSingleNode("//field[@type='SN']" );
But it fail to get the field element. Then I try to delete the
xmlns="http://tempuri.org/fieldValidate.xsd" in validate field, the code
works. After reading some doc, it said:
There is no namespace support for XPath queries specified directly in the
URL. If you want to use a namespace in an XPath query, template should be
used.
I am new in XML, I hope I havn't lead you to a wrong direction

Thanks!

Nov 13 '05 #2

P: n/a
You are almost there. Use SelectionNamespaces property to declare namespace
"a" and use "a:field"

Set o = CreateObject("MSXML2.DOMDocument.3.0")
o.async = false
o.load("my.xml")
o.setProperty "SelectionLanguage", "XPath"
o.setProperty "SelectionNamespaces",
"xmlns:a='http://tempuri.org/fieldValidate.xsd'"
WScript.echo o.selectSingleNode("//a:field[@type='SN']").xml

One word of caution though // is a big performance hit. I you use full path
you would get better perf out of your apps. So use

/a:validate/a:field[@type='SN']

as your xpath query.

Regards,
- Umut Alev

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"dc" <a@a.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
i have a xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<validate xmlns="http://tempuri.org/fieldValidate.xsd">
<field name="Short Name" type="SN" length="10">
<requiredChar value="Y" errMsg="required Y" />
<requiredChar value="X" errMsg="required X" />
<bannedChar value="Z" errMsg="banned Z" />
<bannedChar value="A" errMsg="banned A" />
</field>
<field name="Name" type="N" length="250">
<requiredChar value="Y" errMsg="required Y" />
<requiredChar value="X" errMsg="required X" />
<bannedChar value="Z" errMsg="banned Z" />
<bannedChar value="A" errMsg="banned A" />
</field>
</validate>

And I want to get XmlElement of field which field type is SN. Then I used
this.xmlDoc.SelectSingleNode("//field[@type='SN']" );
But it fail to get the field element. Then I try to delete the
xmlns="http://tempuri.org/fieldValidate.xsd" in validate field, the code
works. After reading some doc, it said:
There is no namespace support for XPath queries specified directly in the
URL. If you want to use a namespace in an XPath query, template should be
used.
I am new in XML, I hope I havn't lead you to a wrong direction

Thanks!

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.