Solved

XPath Question

Posted on 2006-11-11
3
345 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
[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
  • 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

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!

Question has a verified solution.

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

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…
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. 
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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