Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2006, 04:21 AM
Registered User
 
Join Date: Aug 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Confused getting key/(following value) xpath

Hi Gurus,

I have a problem that despite many hours trying to solve, is still puzzling me. I'm hoping one of you can point me in the right direction. I have this cutdown xml..

<dict>
    <key>bus_speed</key>
    <string>167 MHz</string>
    <key>cpu_type</key>
    <string>PowerPC G4 (1.2)</string>
    <key>current_processor_speed</key>
    <string>1.25 GHz</string>
    <key>l2_cache_size</key>
    <string>512 KB</string>
    <key>number_processors</key>
    <integer>1</integer>
    <key>physical_memory</key>
    <string>512 MB</string>
    <key>serial_number</key>
    <string>YM514124RHR</string>
</dict>

I'm confused as to how to choose the value of a string associated with a key. For example what would be the xpath for finding the string (512MB) associated with the key with a value of 'physical_memory'?

I have tried...

dict/key/self::node()['physical_memory']/following-sibling::*

but I get '167 MHz' and not '512 MB'. I cannot use position numbers as I don't know how many key/value pairs there will be. Any help in this matter would be greatly appreciated.

Regards,

Richard

Richard
 
Old September 18th, 2006, 04:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You could use:
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="itemKey" select="'cpu_type'"/>

<xsl:template match="/">
  <data><xsl:value-of select="(dict/key[. = $itemKey]/following-sibling::string)[1]"/></data>
</xsl:template>
</xsl:stylesheet>
or you could use a key for efficiency:
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="itemKey" select="'cpu_type'"/>
  <xsl:key name="valueFromKey" match="dict/string" use="preceding-sibling::key[1]"/>
<xsl:template match="/">
  <data><xsl:value-of select="key('valueFromKey', $itemKey)"/></data>
</xsl:template>

</xsl:stylesheet>
In general selecting by position is a pain. Much easier if your dictionary was something like:
Code:
<dict>
  <item key="bus_speed">167 MHz</item>
  <item key="cpu_type">PowerPC G4  (1.2)</</item>
</dict>


--

Joe (Microsoft MVP - XML)
 
Old September 18th, 2006, 04:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is not a particularly good way of designing the XML, but you can get the answer with

dict/key[.='physical_memory']/following-sibling::*[1]



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 18th, 2006, 06:48 PM
Registered User
 
Join Date: Aug 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks guys! As for the design of the XML, I totally agree that it isn't ideal. Unfortunately, I don't have a choice about it as it is the output of another program.
Thanks again!:D

Richard





Similar Threads
Thread Thread Starter Forum Replies Last Post
<bean:message key="PNR.INPUT"/> key has null value warsha_14 Struts 1 November 13th, 2006 07:26 AM
TAB KEY working together KEY PRESS event thomaz C# 4 August 20th, 2006 02:47 PM
confused mohabedalgani VB.NET 2002/2003 Basics 3 March 30th, 2005 11:48 PM
I'm Confused. Help!!! dangel75 General .NET 1 July 10th, 2004 09:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.