Solved

XPath Question

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

Independent Software Vendors: 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

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…
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'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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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