Solved

XPath Question

Posted on 2006-11-11
3
327 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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