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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by hermanpeckel View Post
    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>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    hermanpeckel (09-27-2012)


  •  

    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
    •