Solved

XPath Question

Posted on 2006-11-11
3
307 Views
Last Modified: 2012-06-21
I need to select MarkNameNodes (with MarkObjectType='TEXT') and its LaserFontNodes, InkFontNodes from a xml document. If a LaserFont or InkFont nodes are not found, display '-".

I have this code :

var MarkNameNodes=theRoot.selectNodes("//MarkTemplate["0"]/MarkingObjects/MarkObject[MarkObjectType='TEXT']/MarkName");
var LaserFontNodes=theRoot.selectNodes("//MarkTemplate["0"]/MarkingObjects/MarkObject[MarkObjectType='TEXT']/LaserFont");
var InkFontNodes=theRoot.selectNodes("//MarkTemplate["0"]/MarkingObjects/MarkObject[MarkObjectType='TEXT']/InkFont");

var strMarkObjectTable="<table border='1' id='MarkObjectGroup'>";
strMarkObjectTable += "<tr><td>MarkName</td><td>Laser</td><td>Ink</td></tr>";

for (i=0;i<MarkNameNodes.length;i++){
      if ( MarkNameNodes.item(i) != null ) {
              strMarkObjectTable += "<tr><td>"+ MarkNameNodes.item(i).text +"</td>";
      } else {
                strMarkObjectTable += "<td>-</td>";
      }

       if (LaserFontNodes.item(i) != null ) {
              strMarkObjectTable += "<tr><td>"+ LaserFontNodes.item(i).text +"</td>";
      } else {
                strMarkObjectTable += "<td>-</td>";
      }

       if ( InkFontNodes.item(i) != null ) {
              strMarkObjectTable += "<tr><td>"+ InkFontNodes.item(i).text +"</td>";
      } else {
                strMarkObjectTable += "<td>-</td>";
      }

       strMarkObjectTable += "</tr>";
}
strMarkObjectTable += "</table><br>";
divMarkObject.innerHTML=strMarkObjectTable;          


it returns me the result of :

MarkName     Laser      Ink
----------------------------------
001               12           12
002               12            9
003               -            -

the correct result should be :  
MarkName     Laser      Ink
----------------------------------
001                -            12
002               12            9
003               12            -

this is the xml :

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMarkTemplate>
  <MarkTemplate>
    <MarkType>MARKTYPE001</MarkType>
    <ImageId>854</ImageId>
    <MarkingObjects>
      <MarkObject>
        <MarkName>001</MarkName>
        <MarkObjectType>TEXT</MarkObjectType>
        <InkFont>12</InkFont>
      </MarkObject>
      <MarkObject>
        <MarkName>002</MarkName>
        <MarkObjectType>TEXT</MarkObjectType>
        <LaserFont>12</LaserFont>
        <InkFont>9</LaserFont>
       </MarkObject>
      <MarkObject>
        <MarkName>003</MarkName>
        <MarkObjectType>TEXT</MarkObjectType>
        <LaserFont>12</LaserFont>      
      </MarkObject>
      <MarkObject>
        <MarkName>004</MarkName>
        <MarkObjectType>GRAPHIC</MarkObjectType>
        <ObjectText>GRAPHIC001</ObjectText>
         <InkFont>10</InkFont>
      </MarkObject>
    </MarkingObjects>
  </MarkTemplate>
  <MarkTemplate>
   ......
  </MarkTemplate>

</ArrayOfMarkTemplate>

Please advise in what is the right way to code to get the correct result.
0
Comment
Question by:sherly
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17926138
Hi sherly,

selectNodes gives you the set of nodes that do exist
so your result is a pretty logical one

why don't you select the MarkObject
and access the childNodes, if existent as childnodes of this MarkObject node
Then you are sure that the allignment is correct

Cheers!
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 250 total points
ID: 17926537
sherly,

here is an example (I simply add nodenames and content to a string "test")

         var MarkObjectNodes=oXML.selectNodes("//MarkTemplate[1]/MarkingObjects/MarkObject[MarkObjectType='TEXT']");
                    for (i=0; i<MarkObjectNodes.length; i++)
                    {
                        for (j=0; j<MarkObjectNodes[i].childNodes.length; j++)
                              {
                                  test = test + (MarkObjectNodes[i].childNodes[j].nodeName);
                                  test = test + ("=");
                                  test = test + (MarkObjectNodes[i].childNodes[j].childNodes[0].nodeValue);
                                  test = test + ("<br />");
                              }                        
                    }        

you can fill in your logic yourself, by testing for the nodeName and adding the value or a "-"

cheers

Geert
0
 
LVL 12

Accepted Solution

by:
jkmyoung earned 250 total points
ID: 17951363
I believe you'd want something like this, with <tr> tags, and <td> tags
Each of the 3 nodes are tied to the particular object node.
Also, the data is replaced with '-' if the node is empty.

var MarkObjectNodes=oXML.selectNodes("//MarkTemplate[1]/MarkingObjects/MarkObject[MarkObjectType='TEXT']");
for (i=0; i<MarkObjectNodes.length; i++)
{
        strMarkObjectTable += "<tr>";            
      var MarkObjectNode = MarkObjectNodes.Item(i);
      var MarkNameNode   = MarkObject.SelectSingleNode("MarkName");
      var LaserFontNode  = MarkObject.SelectSingleNode("LaserFontNode");
      var InkFontNode    = MarkObject.SelectSingleNode("InkFontNode");


        strMarkObjectTable += "<td>";                  
        if (MarkNameNode != null) {
            strMarkObjectTable += MarkNameNode.text;
        } else {
                strMarkObjectTable += "-";
        }
        strMarkObjectTable += "</td>";                  

        strMarkObjectTable += "<td>";                  
        if (LaserFontNode != null) {
            strMarkObjectTable += LaserFontNode.text;
        } else {
                strMarkObjectTable += "-";
        }
        strMarkObjectTable += "</td>";            

        strMarkObjectTable += "<td>";                        
        if (InkFontNode != null) {
            strMarkObjectTable += InkFontNode.text;
        } else {
                strMarkObjectTable += "-";
        }
        strMarkObjectTable += "</td></tr>";            
}
0

Featured Post

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.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

10 Experts available now in Live!

Get 1:1 Help Now