Topic
  • 6 replies
  • Latest Post - ‏2012-09-11T01:19:19Z by hermanpeckel
hermanpeckel
hermanpeckel
4 Posts

Pinned topic XPath Newbie - Get Variable Value

‏2012-09-07T00:03:26Z |
Folks,

Firstly, apologies for what is most likely a newbie question, but I'm really stuck on this. Google hasn't saved me either.

I need to get the value of the "MediaBox" - "Width" variable (610)

in this XML using an XPath.

{<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>}

I have tried using -

{//od:PageBoxInfo/Page/PageBox/Width}

...but no cigar!

Anyone have any ideas? I've tried a few things but nothing seems to work.

Any help much appreciated!

Thanks!
HP
Updated on 2012-09-11T01:19:19Z at 2012-09-11T01:19:19Z by hermanpeckel
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: XPath Newbie - Get Variable Value

    ‏2012-09-07T07:45:49Z  
    Width is an attribute so it needs to appear as @Width in your path. And you need a predicate to select the MediaBox:

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


    You also need to ensure that the "od" namespace prefix is bound to the same namespace in your source document and in your query.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: XPath Newbie - Get Variable Value

    ‏2012-09-07T07:47:19Z  
    Width is an attribute so it needs to appear as @Width in your path. And you need a predicate to select the MediaBox:

    <pre class="jive-pre"> //od:PageBoxInfo/Page/PageBox[@BoxType="MediaBox"]/Width </pre>

    You also need to ensure that the "od" namespace prefix is bound to the same namespace in your source document and in your query.
    Sorry, got that wrong

    
    //od:PageBoxInfo/Page/PageBox[@BoxType="MediaBox"]/@Width
    
  • hermanpeckel
    hermanpeckel
    4 Posts

    Re: XPath Newbie - Get Variable Value

    ‏2012-09-10T00:06:55Z  
    Sorry, got that wrong

    <pre class="jive-pre"> //od:PageBoxInfo/Page/PageBox[@BoxType="MediaBox"]/@Width </pre>
    Ahhhh!!! Brilliant!! Thanks heaps Michael.

    One more question though - is anything needed in order to reference "Width" as a an integer rather than a string? As in, do I need to declare the @Width variable as an integer somehow?
  • hermanpeckel
    hermanpeckel
    4 Posts

    Re: XPath Newbie - Get Variable Value

    ‏2012-09-10T00:15:20Z  
    Ahhhh!!! Brilliant!! Thanks heaps Michael.

    One more question though - is anything needed in order to reference "Width" as a an integer rather than a string? As in, do I need to declare the @Width variable as an integer somehow?
    Actually, I lied, I've got more questions as for some reason the XPath is not working.

    Does the ID="Link6" (first line of code below) need to be referenced in my xpath?

    
    <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>
    


    Thanks again for your help and apologies for having no idea.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: XPath Newbie - Get Variable Value

    ‏2012-09-10T07:28:17Z  
    Actually, I lied, I've got more questions as for some reason the XPath is not working.

    Does the ID="Link6" (first line of code below) need to be referenced in my xpath?

    <pre class="jive-pre"> <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> </pre>

    Thanks again for your help and apologies for having no idea.
    If the od:PageBoxInfo is the only od:PageBoxInfo in your document then you don't need the ID to disambiguate it.

    If you're using a schema-aware XPath 2.0 transformation then @Width will automatically be the type declared in the schema. Otherwise, you may need to convert it explicitly, using number() in XPath 1.0 or xs:integer() in XPath 2.0. However, conversions often happen automatically, depending on the context where the value is used.
  • hermanpeckel
    hermanpeckel
    4 Posts

    Re: XPath Newbie - Get Variable Value

    ‏2012-09-11T01:19:19Z  
    If the od:PageBoxInfo is the only od:PageBoxInfo in your document then you don't need the ID to disambiguate it.

    If you're using a schema-aware XPath 2.0 transformation then @Width will automatically be the type declared in the schema. Otherwise, you may need to convert it explicitly, using number() in XPath 1.0 or xs:integer() in XPath 2.0. However, conversions often happen automatically, depending on the context where the value is used.
    Hi Michael,

    All of what you explained makes sense. Thanks a million for your help!

    HP