![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: Problem with an XPATH statement in JScript [Thread Next] Re: Problem with an XPATH statement in JScriptTo: NULL Date: 7/8/2006 8:32:00 PM
"Michael D." <MichaelD@d...> wrote in message
news:CA98839D-0FDF-4136-B106-BB2FF276DA92@m......
>
> "Martin Honnen" wrote:
>
> >
> >
> > Michael D. wrote:
> >
> > > I'm trying to select a node by it's position number in a JScript
statement.
> > > However, the below line gives me an error:
> > >
> > > myInfoNode2 = xmlCourse.selectSingleNode("position()=3");
> >
> > I think you want
> > xmlCourse.selectSingleNode("node()[position()=3]")
> > or simply
> > xmlCourse.selectSingleNode("node()[3]")
> > Depening on what kind of node xmlCourse is you also want to look for
> > descendants and not only children e.g.
> > xmlCourse.selectSingleNode(".//node()[3]")
> >
> > --
> >
> > Martin Honnen --- MVP XML
> > http://JavaScript.FAQTs.com/
> >
>
> Thanks, that was helpful. I'm not quite where I want to be yet though.
I'm
> trying to create a sort of navigating scheme for an online course
template, I
> was hoping to get an XPATH statement that could be used to get to any node
> regardless of it's type or position in the document.
>
> All the nodes (except the document element) will have a "link" attribute
> with a webpage url (the example below is just dummy data, so all the link
> attributes are not unique)
>
> Given the example XML data below, could I construct an XPATH that would
give
> me (for example):
>
> xmlCourse.selectSingleNode(".//node()[1]") = the <courseInfo> node
> xmlCourse.selectSingleNode(".//node()[2]") = the first <module> node
> xmlCourse.selectSingleNode(".//node()[3]") = the first<lesson> under the
> first <module>
> xmlCourse.selectSingleNode(".//node()[4]") = the first <topic> under the
> first <lesson>
> xmlCourse.selectSingleNode(".//node()[5]") = the first <page> under the
> first <topic>
> xmlCourse.selectSingleNode(".//node()[6]") = the SECOND <page>
> xmlCourse.selectSingleNode(".//node()[7]") = the THIRD <page>
>
> etc, etc...
>
You need the descendant axis:-
xmlCourse.selectSingleNode("descendant::*[1]")
However rather than running lots of xpath queries better would be:-
var oNodes = xmlSource.selectNodes("decendant::*")
oNodes.item(0)
oNodes.item(1)
etc, etc...
> Here is a simple example of the XML I'm trying to navigate:
>
> <courseInfo courseName="Test Course" courseCode="009999">
> <module name="Module One" link="module1.html">
> <lesson name="M1 Lesson One" link="lesson1.html">
> <topic link="topic1.html" name="Module One Lesson One Topic One">
> <page link="link.html" name="M1L1T1Page1"/>
> <page link="link.html" name="M1L1T1Page2"/>
> <page link="link.html" name="M1L1T1 Page Three"/>
> <page link="link.html" name="M1L1T1Page Four"/>
> </topic>
> <topic name="Module One Lesson One Topic Two" link="topic2.html">
> <page name="Page One" link="link.html"/>
> </topic>
> </lesson>
> <lesson name="Module One Lesson Two" link="link.html">
> <topic name="TOPIC ONE IN THAT MODULE" link="link.html">
> <page name="one" link="link.html"/>
> <page name="two" link="link.html"/>
> </topic>
> </lesson>
> </module>
> </courseInfo>
>
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
