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 December 31st, 2008, 05:44 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default having issue printing child values by comparing parents value

Here is my XML
<root>
<value>
<item name="key">
<value>Test</value>
</item>
<item name="label">
<value>&lt;b&gt;Test&lt;/b&gt;</value>
</item>
<item name="foldername">
<value>Test</value>
</item>
<value>
<item name="key">
<value>Test1.1</value>
</item>
<item name="label">
<value>Test 1.1</value>
</item>
<item name="foldername">
<value/>
</item>
</value>
<value>
<item name="key">
<value>Test1.2</value>
</item>
<item name="label">
<value>Test 1.2</value>
</item>
<item name="foldername">
<value/>
</item>
</value>
</item>
<item name="key">
<value>Sample</value>
</item>
<item name="label">
<value>&lt;b&gt;Sample&lt;/b&gt;</value>
</item>
<item name="foldername">
<value>Sample</value>
</item>
</value>
</root>



=============================================
I am trying to print just this as the output

PARENT key=Test label=Test foldername=Test
CHILD Key=Test1.1 label=Test 1.1 foldername=
CHILD Key=Test1.2 label=Test 1.2 foldername=

I know that in order to get the value I need to use
<xsl:value-of select="//item/value"/>
What I dont know is how do I print out the output thats needed by providing just Test as my item name key.
 
Old December 31st, 2008, 06:05 PM
Authorized User
 
Join Date: Dec 2008
Posts: 50
Thanks: 1
Thanked 5 Times in 5 Posts
Default

Code:
<xsl:value-of select="//item/@name"/>
 
Old January 1st, 2009, 06:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's very hard to discern the structure of your input as there is no indentation. I tried pasting it into an XML editor to get it indented, and it seems that it isn't even well-formed, which makes it even harder to see the intended structure! Also it doesn't help that you have used "Test" as the value of many different fields, which makes it difficult to see which parts of your output are supposed to come from which parts of your input.

Escaping the nested markup within the <value> elements isn't going to make your life easier either.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 5th, 2009, 03:37 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default Here is the correct XML

<?xml version="1.0"?>
<root>
<value>
<item name="key">
<value>News</value>
</item>
<item name="label">
<value>&lt;b&gt;News&lt;/b&gt;</value>
</item>
<item name="foldername">
<value>news</value>
</item>
<value>
<item name="key">
<value>Sportsnews</value>
</item>
<item name="label">
<value>Sports News</value>
</item>
<item name="foldername">
<value/>
</item>
</value>
<value>
<item name="key">
<value>FinancialNews</value>
</item>
<item name="label">
<value>Financial News</value>
</item>
<item name="foldername">
<value/>
</item>
</value>
<item name="key">
<value>Classifieds</value>
</item>
<item name="label">
<value>&lt;b&gt;classifieds&lt;/b&gt;</value>
</item>
<item name="foldername">
<value>sell</value>
</item>
</value>
</root>



I need just the News and its children values to be printed out for example.
Parent key value = News
children key value = Sportsnews
children key value = FinancialNews

I hope this makes sense.
The Following User Says Thank You to eruditionist For This Useful Post:
mrame (January 7th, 2009)
 
Old January 7th, 2009, 02:34 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

Try the below code. This is hard-coded for two value elements as per the input. There can be better solution.
Code:
<xsl:template match="root">
<xsl:apply-templates select="value" mode="value"></xsl:apply-templates>
</xsl:template>

<xsl:template match="value" mode="value">
<xsl:apply-templates select="item" mode="item"></xsl:apply-templates>
</xsl:template>

<xsl:template match="item" mode="item">
<xsl:if test="value = 'News'">
Parent key value = <xsl:value-of select="value"></xsl:value-of>
Children key value = <xsl:value-of select="../value[1]/item[@name = 'key']/value"></xsl:value-of>
Children key value = <xsl:value-of select="../value[2]/item[@name = 'key']/value"></xsl:value-of>
</xsl:if>
</xsl:template>

__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing Two values from same table MArgente BOOK: Beginning SQL 3 December 11th, 2009 07:35 AM
Comparing values between different MSHFlexGrid davekrunal46 VB How-To 0 November 28th, 2005 07:41 AM
Comparing values between different MSHFlexGrid davekrunal46 VB Databases Basics 0 November 28th, 2005 07:39 AM
Comparing values between different MSHFlexGrid davekrunal46 Beginning VB 6 0 November 28th, 2005 07:38 AM
Comparing Dates Issue tdaustin Classic ASP Basics 2 June 2nd, 2004 06:30 PM





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