CodingForums.com

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

hermanpeckel 09-05-2012 06:36 AM

Variable Referencing via XPath
 
Folks,

After kicking an absolute goal on this forum last time (or having that goal kicked for me) I was hoping to get some more help with my latest XPath conundrum.

I need to reference the values of an XML variable in this code via an XPath -

Code:

<od:PageBoxInfo ID="Link6">
<Page>
<PageBox BoxType="MediaBox" Width="594.999" Height="1280"/>
<PageBox BoxType="CropBox" Width="594.999" Height="1280"/>
<PageBox BoxType="BleedBox" Width="594.999" Height="1280"/>
<PageBox BoxType="ArtBox" Width="594.999" Height="1280"/>
<PageBox BoxType="TrimBox" Width="594.999" Height="1280"/>
</Page>
</od:PageBoxInfo>

I'm trying to get the width of the MediaBox. I'm assuming it will be something like -

//od:PageBoxInfo/Page/PageBox/BoxType='MediaBox'/Width

...but, as I've mentioned before, I can barely spell XPath. Any help much appreciated!!

Thanks in advance
HP

Arbitrator 09-10-2012 03:47 AM

Quote:

Originally Posted by hermanpeckel (Post 1267073)
I'm trying to get the width of the MediaBox.

Given the XML:
Code:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="master.xslt"?>
<od:PageBoxInfo ID="Link6" xmlns:od="http://www.codingforums.com/showthread.php?t=272271">
        <Page>
                <PageBox BoxType="MediaBox" Width="594.999" Height="1280"/>
                <PageBox BoxType="CropBox" Width="594.999" Height="1280"/>
                <PageBox BoxType="BleedBox" Width="594.999" Height="1280"/>
                <PageBox BoxType="ArtBox" Width="594.999" Height="1280"/>
                <PageBox BoxType="TrimBox" Width="594.999" Height="1280"/>
        </Page>
</od:PageBoxInfo>

You can get the relevant value with the following XSLT style sheet:

Code:

<?xml version="1.0" encoding="utf-8"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xslt="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <!-- |method="xml"| required for standards mode in Windows Internet Explorer 9.00.8112.16421 Update Versions 9.0.9 x64 -->
        <output method="xml" media-type="application/xhtml+xml"/>
        <template match="/">
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
                        <head>
                                <title>Demo Document</title>
                                <style>
                                        /* Layout */
                                        * { margin: 0; padding: 0; }
                                        html, p { margin: 1em; }
                                </style>
                        </head>
                        <body>
                                <!-- Longform XPath Demo -->
                                <p>The media box width is <xslt:value-of select="descendant-or-self::node()/child::PageBox[attribute::BoxType='MediaBox']/attribute::Width"/> units.</p>
                                <!-- Shortform XPath Demo -->
                                <p>The media box width is <xslt:value-of select="//PageBox[@BoxType='MediaBox']/@Width"/> units.</p>
                        </body>
                </html>
        </template>
</stylesheet>



All times are GMT +1. The time now is 09:29 AM.

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