Solved

Select Nodes with Names Like ?

Posted on 2006-11-11
9
315 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Technology Partners: 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!

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, …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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