0

I am not familiar with xml. I have an xml document structured like below:

<?xml version="1.0" encoding="UTF-8"?>
<a:b xmlns="something">
  <a:c>
    <d>
      <e>
        <item>item1</item>
        <item>item1</item>
        <item>item1</item>
       </e>
     </d>
   <a:c>
 <a:b>

I want to get the node "e" to retrieve its child items in my xslt as below:

<xsl:variable name="Product" select="document('itemList.xml')/node()[1]/node()[0]/node()[0]/node()[0]"/>

But it is not working. Kindly suggest the right way to do it. Also, is the first node refered by node()[0] or node()[1]? Links to articles for a good understanding of this node concept of xml are welcome.

2
  • Is the /node()[1]/node()[0]/node()[0]/node()[0] XPATH? I think maybe you can replace it with //item or else /a:b/a:c/d/e/item Commented Jan 24, 2013 at 8:07
  • Also you asked for links: class2go.stanford.edu/db/Winter2013 check out the videos of the querying XML section. Watch at least the XPATH and the XSLT video. Commented Jan 24, 2013 at 8:09

2 Answers 2

1

Your XML doesn't have the prefix a bound to an URI. Assuming that is fixed.

a:b/a:c/x:d/x:e

will get you the node when x is bound to something

Sign up to request clarification or add additional context in comments.

3 Comments

but to get all the children shouldn't you add a /item at the end there (or a /x:item)?
@Dan x:e provides the entire sub-tree. You'll have the item in it. When you have /item appended at last of your XPath, you'll be getting a list of item nodes, not e.
Yeah that's my interpretation of the OPs question though. Even though he says he wants node e I think he really wants a list of the item elements. He says he wants to retrieve the child elements of node e. Either way I'm sure this will get him to the right place. +1
0

The XML you provided is not currently valid. It has declared a default namespace, but has not declared the a: namespace. It would need to start with something like this:

<a:b xmlns="something" xmlns:a="somethingElse">

If, in your XSLT, you declared the a namespace and associated the something namespace with the prefix s, you could access the e node with:

/a:b/a:c/s:d/s:e

If you want to access the nodes simply based on their position, you could do this, though this is not usually a very good practice:

/*[1]/*[1]/*[1]/*[1]

To answer your question, XPath is 1-index based, so the first item in any selection is accessed with [1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.