Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select Nodes with Names Like ?

Posted on 2006-11-11
9
Medium Priority
?
319 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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