Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XPath Question

Posted on 2006-11-11
3
Medium Priority
?
349 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 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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 video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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