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 August 12th, 2010, 03:23 AM
Authorized User
 
Join Date: Aug 2010
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default template for child nodes

Hi,
This is my first try at xslt. <template> is an entry point for xslt. How can i use it to fetch only child node data without complete root's/document node's data?

Thanks in advance
 
Old August 12th, 2010, 03:35 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Hi

Could you please read this post on how to write a good post here: Hints for a good XSLT post

Then try showing us what your input XML looks like and what you'd like your output XML to look like and what you've tried and we might be able to help you.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 12th, 2010, 04:49 AM
Authorized User
 
Join Date: Aug 2010
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default sample xml and my xslt

Sam,
Thanks for replying.

The xml source is,

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="OrderPrice.xslt"?>
<OrderInfo>
<Customer>
<AccountNumber>7723123</AccountNumber>
</Customer>
<LineItems>
<Item no="1">
<SKU>1234</SKU>
<Quantity>1</Quantity>
<PriceCode>E</PriceCode>
</Item>
<Item no="2">
<SKU>7723</SKU>
<Quantity>2</Quantity>
<PriceCode>C</PriceCode>
</Item>
</LineItems>
</OrderInfo>



The xslt is as follows


<xsl:template match="Item">
<xsl:variable name="letter" select="PriceCode"/>
<Item>
<SKU>
<xsl:value-of select="SKU"></xsl:value-of>
</SKU>
<Quantity>

<xsl:value-of select="Quantity"></xsl:value-of>

</Quantity>
<Price>
<xsl:value-of select="$prices/Code[Letter=$letter]/Price"/>
</Price>
</Item>


</xsl:template>


This gives me the whole data from xml file. How can I extract data only from single node, price, item etc.
 
Old August 12th, 2010, 04:52 AM
Authorized User
 
Join Date: Aug 2010
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default root

also this xslt does not define root element for my output. Kindly help
 
Old August 12th, 2010, 05:00 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This instruction looks pretty random

Code:
<xsl:value-of select="$prices/Code[Letter=$letter]/Price"/>
because there are no elements called Code, Letter, or Price in your input, nor do you declare a variable called $prices.

Apart from that, the problem seems to be that you need a template rule to match the root node. Something like

Code:
<xsl:template match="/">
  <output>
     <xsl:apply-templates select="//Item[@no='2']"/>
  </output>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 12th, 2010, 05:48 AM
Authorized User
 
Join Date: Aug 2010
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for the reply,


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>


<xsl:variable name="prices" select=
"document('Price.xml')/PriceCodes"/>

<xsl:template match ="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="OrderInfo/Customer">
<Customer>
<AccountNumber>
<xsl:value-of select="AccountNumber"/>

</AccountNumber>
</Customer>

</xsl:template>


<xsl:template name="lineitems" match="/OrderInfo/LineItems/Item">
<xsl:variable name="letter" select="PriceCode"/>
<Item>
<SKU>
<xsl:value-of select="SKU"></xsl:value-of>
</SKU>
<Quantity>
<xsl:value-of select="Quantity"></xsl:value-of>
</Quantity>
<Price>
<xsl:value-of select="$prices/Code[Letter=$letter]/Price"/>
</Price>
</Item>
</xsl:template>

</xsl:stylesheet>




The output i want is


<root>

<Customer><AccountNumber>7723123</AccountNumber></Customer>

<Item>
<SKU>1234</SKU>
<Quantity>1</Quantity>
<Price>15.50</Price>
</Item>

<PriceCode ItemPrice="15.50" />

<Item>
<SKU>7723</SKU>
<Quantity>2</Quantity>
<Price>6.75</Price>
</Item>

<PriceCode ItemPrice="6.75" />

</root>



The <price> gets proper value from other xml file.
How can i get the root element.
Thanks for helping.
 
Old August 12th, 2010, 05:59 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Simply change:

Code:
<xsl:template match ="/">
<xsl:apply-templates/>
</xsl:template>
to

Code:
<xsl:template match ="/">
<root>
  <xsl:apply-templates/>
</root>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 12th, 2010, 06:02 AM
Authorized User
 
Join Date: Aug 2010
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default Thanks

That solved my problem. Thank you





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the size of the child nodes. eruditionist XSLT 2 November 7th, 2008 11:59 AM
not able to print out the child nodes eruditionist XSLT 7 October 30th, 2008 10:33 AM
text and child nodes eepyoga XSLT 1 April 8th, 2008 12:45 PM
Trying to group child nodes aalbetski XSLT 3 November 18th, 2006 01:29 PM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM





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