Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    XPath Referencing Integer

    Folks,

    I've had some success with my XPath forays, mostly thanks to people on this site. I thought I'd try my luck again!

    I need to access the value of the WIDTH attribute of the TRIM BOX in -

    Code:
    <od:PageBoxInfo ID="Link6">
    <Page>
    <PageBox BoxType="MediaBox" Width="610" Height="1280"/>
    <PageBox BoxType="CropBox" Width="592" Height="1280"/>
    <PageBox BoxType="BleedBox" Width="593" Height="1280"/>
    <PageBox BoxType="ArtBox" Width="594" Height="1280"/>
    <PageBox BoxType="TrimBox" Width="595" Height="1280"/>
    </Page>
    </od:PageBoxInfo>
    ...and I have tried to do this using the XPath -

    //od:PageBoxInfo/Page/PageBox[@BoxType="TrimBox"]/@Width

    but to no avail. Does this XPath look correct?

    I was thinking that the result may have been returned to me as a string rather than an integer, but I'm not sure. I'm no XPath master!

    Any ideas?

    Thanks in advance
    HP

  • #2
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Actually, I just noticed the double quotes (") in my XPath. I changed this to single quotes (') but still the same result.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,100
    Thanks
    23
    Thanked 594 Times in 593 Posts
    I am getting error due to the od: in the xml file. This maybe your problem.

    May I offer an easier method, simplexml. Remove both od: and this does what you want
    PHP Code:
    <?php
    $xml 
    simplexml_load_file("PageBox.xml");  // I made your code into a file named People.xml

    $i 0;
    foreach (
    $xml->Page->PageBox as $PageBox[])
    {
        if(
    $PageBox[$i]->attributes()->BoxType == "TrimBox")
            echo 
    "The Width you seek is: ".$PageBox[$i]->attributes()->Width "<br />";
        
    $i++;
    }
    ?>

  • #4
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your help Sunfighter. Unfortunately the XML is generated by another application we are working with and we have no option to change it. The XML is actually a Job Definition File (.jdf) and contains about 2000 lines of code (most of it useless).

    It's interesting what you say about the "od:" as this appears in a lot of the file. Is there a way I can reference things without the "od:"?

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,100
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Maybe you could try something like this first
    PHP Code:
    <?php
    $string 
    file_get_contents("PageBox.jdf");
    $string str_replace("od:"""$string);
    file_put_contents("PageBox.xml"$string);
    ?>
    And then run what I gave you before. I don't know the real names for your files so adjust accordingly.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    I don’t think it’s a good idea to manually remove an XML namespace, they are there for a reason.

    to the original problem: the XPath so far is correct, but the od namespace must be known to the XPath interpreter (which obviously depends on the interpreter used, XSLT is different from SimpleXML is different from DOMDocument, etc.)

    the return type of the XPath expression is usually a string for primitive data types (maybe except for the XPath functions), though that may also depend on the interpreter used.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your input guys!

    I just heard back from our software developers who support the product that uses the Xpath info. Apparently the software does not support Xpath 2.0, and only supports a subset of Xpath 1.0, and they can't tell me what that subset is.

    I assume this is what is making things difficult (or impossible). Sorry to waste your time guys.

    Regards

    HP


  •  

    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
    •