Solved

Select Nodes with Names Like ?

Posted on 2006-11-11
9
312 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 300 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with SimpleXML charts with PHP 4 93
Test ddwrt:UserLookup 1 63
Convert XML to excel12book 5 29
XML SQL 8 25
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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. 
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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