CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   XML (http://www.codingforums.com/forumdisplay.php?f=3)
-   -   XPath Referencing Integer (http://www.codingforums.com/showthread.php?t=282047)

hermanpeckel 11-12-2012 11:15 PM

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

hermanpeckel 11-13-2012 02:11 AM

Actually, I just noticed the double quotes (") in my XPath. I changed this to single quotes (') but still the same result.

sunfighter 11-16-2012 07:57 PM

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++;
}
?>


hermanpeckel 11-18-2012 09:50 PM

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:"?

sunfighter 11-19-2012 06:37 AM

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.

Dormilich 11-19-2012 02:34 PM

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.

hermanpeckel 11-20-2012 10:54 PM

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. :confused:

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

Regards

HP


All times are GMT +1. The time now is 12:33 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.