Solved

Select Nodes with Names Like ?

Posted on 2006-11-11
9
310 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
Comment Utility
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
Comment Utility
I'm having an unknown method error, please advise.
0
 
LVL 2

Author Comment

by:sherly
Comment Utility
Error : Unknown method
//MarkTemplate[0]/*[-->starts-with(name<--(),'Image')]
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Comment

by:sherly
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now