www.webdeveloper.com
Results 1 to 7 of 7

Thread: How to parse node value by parent element attribute?

  1. #1
    Join Date
    Dec 2008
    Posts
    42

    How to parse node value by parent element attribute?

    Hi there

    i'm trying to display on an HTML page the value of one record in an XML file on another server. Does anyone know how?

    Code:
    <STOREITEMS>
        <PRODUCT ITEM="0001" NAME="product 1">
            <STOCK>In Stock.</STOCK>
        </PRODUCT>
        <PRODUCT ITEM="0002" NAME="product 2">
            <STOCK>out of stock. </STOCK>
        </PRODUCT>
    </STOREITEMS>
    Does anyone know how to call the data based on an element attribute. ie, i want to call whether ONE product is in stock, based on its 'NAME'.

    Ie; HTML would display "In Stock." on an HTML page that calls the 'STOCK' value for the 'PRODUCT' element with 'NAME' attribute 'product 1' but a different HTML page would display "out of stock." as it would call 'STOCK' value from 'PRODUCT' element with 'NAME' attribute 'product 2'.

    I've been trying to figure this out forever if anyone can help id be really greatful, i guess its probably quite easy?

    Thanks

  2. #2
    Join Date
    May 2008
    Posts
    381
    XPath expression to select the PRODUCT with @NAME matching the value of $name and the child STOCK element containing the word "In":
    Code:
    /STOREITEMS/PRODUCT[@NAME=$name]/STOCK[contains(., "In")]/..
    I'm not sure if that would work, but a quick test suggests it would... Out of curiosity, why not store how many are in stock instead? That way, you could determine if you were out of stock based upon whether the number stockpiled is 0. Then you could store it as an attribute, and the structure wouldn't be so weird.

  3. #3
    Join Date
    Dec 2008
    Posts
    42
    Hi there, thanks for your help.

    Unfortunately its not my xml feed, its from my supplier and its hosted on their servers. I tried what you said but no joy perhaps its because the xml is not hosted on my server, i know browsers have some access rules for security... is there a workaround?

    I have been trying:

    Code:
    <html>
    <body>
    <script type="text/javascript">
    function loadXMLDoc(fname)
    {
    var xmlDoc;
    // code for IE
    if (window.ActiveXObject)
      {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
      {
      xmlDoc=document.implementation.createDocument("","",null);
      }
    else
      {
      alert('Your browser cannot handle this script');
      }
    xmlDoc.async=false;
    xmlDoc.load(fname);
    return(xmlDoc);
    }
    
    xml=loadXMLDoc("http://www.supplier.com/catalog/xml-feed/stock.xml");
    path="/STOREITEMS/PRODUCT[@NAME=$product 1]/STOCK[contains(., "In")]/.."
    // code for IE
    if (window.ActiveXObject)
    {
    var nodes=xml.selectNodes(path);
    
    for (i=0;i<nodes.length;i++)
      {
      document.write(nodes[i].childNodes[0].nodeValue);
      document.write("<br />");
      }
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
    var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
    var result=nodes.iterateNext();
    
    while (result)
      {
      document.write(result.childNodes[0].nodeValue);
      document.write("<br />");
      result=nodes.iterateNext();
      }
    }
    </script>
    
    </body>
    </html>
    Does that look right?

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    JavaScript's too iffy anyway. Better to find a nice server side solution. What server side scripting do you have available?
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  5. #5
    Join Date
    Dec 2008
    Posts
    42

    server-side scripting

    Hi Charles,

    I have coldfusion, perl, python, ruby, php etc...

    php would be best by far for me as i have (some) knowledge of how to use it and the page that i want to use it on is php.

    do you have any ideas for how to do it?

    thanks

    Matt

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    Quote Originally Posted by mastubbs View Post
    do you have any ideas for how to do it?
    Several, but the good folks on the PHP forum will be better able to help you. But they will need to know what version of PHP you have and what modules are installed. (There is a big difference between PHP 4 & 5 in the XML department.)
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  7. #7
    Join Date
    Dec 2008
    Posts
    42

    thanks

    ok thanks mate.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles