Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Select Nodes with Names Like ?

Posted on 2006-11-11
9
Medium Priority
?
318 Views
Last Modified: 2008-02-07
By using xpath, i can retrieve first MarkTemplate's ObjectText with MarkObjectType='TEXT' using this syntax :
var MatchingNodes=theRoot.selectNodes("//MarkTemplate[0]/MarkingObjects/MarkObject[MarkObjectType='TEXT']/ObjectText");

What is the syntax if I want to select all nodes of first MarkTemplate with node name begin with "Image" which select "ImageId1","ImageId2","ImageId3" ?

This is my xml :

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMarkTemplate>
  <MarkTemplate>
    <MarkType>MARKTYPE001</MarkType>
    <ImageId1>854</ImageId1>
    <ImageId2>999</ImageId2>
    <ImageId3>394</ImageId3>
    <MarkingObjects>
      <MarkObject>
        <MarkObjectType>TEXT</MarkObjectType>
        <ObjectText>TEXT001</ObjectText>
        <MarkMethod></MarkMethod>
        <MarkInfo>Info</MarkInfo>
      </MarkObject>
      <MarkObject>
        <MarkObjectType>TEXT</MarkObjectType>
        <ObjectText>TEXT002</ObjectText>
        <MarkInfo>Info</MarkInfo>
       </MarkObject>
      <MarkObject>
        <MarkObjectType>TEXT</MarkObjectType>
        <ObjectText>TEXT003</ObjectText>
       <MarkInfo>Info001</MarkInfo>      
      </MarkObject>
      <MarkObject>
        <MarkObjectType>GRAPHIC</MarkObjectType>
        <ObjectText>GRAPHIC001</ObjectText>
       <MarkInfo>Info004</MarkInfo>
      </MarkObject>
    </MarkingObjects>
  </MarkTemplate>
  <MarkTemplate>
   ......
  </MarkTemplate>
</ArrayOfMarkTemplate>

0
Comment
Question by:sherly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17921065
Hi sherly,

you can use starts-with() on the name()

var MatchingNodes=theRoot.selectNodes("//MarkTemplate[0]/*[starts-with(name(), 'Image')]")

Cheers!
0
 
LVL 2

Author Comment

by:sherly
ID: 17921093
I'm having an unknown method error, please advise.
0
 
LVL 2

Author Comment

by:sherly
ID: 17921106
Error : Unknown method
//MarkTemplate[0]/*[-->starts-with(name<--(),'Image')]
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17921148
sherly,

you have to use a more modern version of the msxml parser
you are likely using msxml3, but in version1 mode, not in version2 mode
you should definitely use the parser in version 2 mode
but it is even better to upgrade to msxml4 (download from microsoft web, it leaves the msxml3 in place, so nothing breaks)
microsoft was too late supporting some of the XPath functions in their parser

Can you show me the line where you create theRoot
(the thing with MSXML.DOM or whatever)
I will then send you an alternative

In the mean time you can test this
var MatchingNodes=theRoot.selectNodes("//MarkTemplate[0]/*[substring(name(), 1, 6) = 'Image']")

cheers

Geert
0
 
LVL 2

Author Comment

by:sherly
ID: 17921165
Hi, I'm very new in XML. These are the only 2 lines which I declared theRoot. My xml data is on the html itself, the <xml id="xmldata"></xml> of the portion. I'm using IE6 and I don't know which version of MSXML I'm using. Please guide me in the proper way to declare the DOM. I'm increasing question point to 300. Thanks for your help.

// initialize objects
this.xmldoc = document.getElementById("xmldata");
                                                            
//Get the root
var theRoot = this.xmldoc.documentElement;
0
 
LVL 2

Author Comment

by:sherly
ID: 17921169
I tried :
var MatchingNodes=theRoot.selectNodes("//MarkTemplate[0]/*[substring(name(), 1, 6) = 'Image']")

but it is returning more elements than I expected.
0
 
LVL 2

Author Comment

by:sherly
ID: 17921198
sorry, I was using var MatchingNodes=("//MarkTemplate[0]/*[substring(name(), 1, 6) = 'Image']") just now. I missed out the Root.selectNodes.

Now, I tried var MatchingNodes=theRoot.selectNodes("//MarkTemplate[0]/*[substring(name(), 1, 6) = 'Image']") and I got unknown method error.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17921217
sherly,

mmh, I am not sure this kind of IE internal processing allows for the proper selection of the parser
IE6 comes with msxml3

I found two little bugs in the XPath
the 0 should be 1 ofcourse
and Image has only 5 characters instead of 6 :-)

This one works perfectly for me
var MatchingNodes=theRoot.selectNodes("//MarkTemplate[1]/*[substring(name(), 1, 5) = 'Image']")
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1200 total points
ID: 17921248
sherly,

well, it seems you also don't have access to the substring() function

I see two options for you here

1. fully express all the names
var MatchingNodes=theRoot.selectNodes("//MarkTemplate[1]/*[name() = 'ImageId1' or name() = 'ImageId2' or name() = 'ImageId3']")

2. make the XML external to the HTML
and call it in, with the proper document object creator,
if that is acceptable for you

         var oXML = new ActiveXObject("Msxml2.DOMDocument.4.0");
         oXML.async = false;
         oXML.resolveExternals = false;
         oXML.load("yourFile.xml");  
         var MatchingNodes=oXML.selectNodes("//MarkTemplate[1]/*[substring(name(), 1, 5) = 'Image']")

If you don't install msxml4,
this could work as well
         var oXML = new ActiveXObject("Msxml2.DOMDocument.3.0");
you would then use msxml3 in version2 mode

cheers

Geert

 
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question