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 26th, 2010, 11:06 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default Creating new XML using XSLT

Hi All,

I Have below xml using XSLT I want to create new xml based on the node name passed in xpath.

Code:
<?xml version="1.0" standalone="yes"?>
<DataSet> 
    <Orders>
        <SalesOrderID>43659</SalesOrderID>
        <SalesOrderDetailID>1</SalesOrderDetailID>
        <CarrierTrackingNumber>4911-403C-98</CarrierTrackingNumber>
        <OrderQty>1</OrderQty>
        <ProductID>776</ProductID>
        <SpecialOfferID>1</SpecialOfferID>
        <UnitPrice>2024.994</UnitPrice>
        <UnitPriceDiscount>0.0000</UnitPriceDiscount>
        <LineTotal>2024.994000</LineTotal>
        <rowguid>b207c96d-d9e6-402b-8470-2cc176c42283</rowguid>
        <ModifiedDate>2001-07-01T00:00:00+01:00</ModifiedDate>
    </Orders>
    <Orders>
        <SalesOrderID>43659</SalesOrderID>
        <SalesOrderDetailID>2</SalesOrderDetailID>
        <CarrierTrackingNumber>4911-403C-98</CarrierTrackingNumber>
        <OrderQty>3</OrderQty>
        <ProductID>777</ProductID>
        <SpecialOfferID>1</SpecialOfferID>
        <UnitPrice>2024.994</UnitPrice>
        <UnitPriceDiscount>0.0000</UnitPriceDiscount>
        <LineTotal>6074.982000</LineTotal>
        <rowguid>7abb600d-1e77-41be-9fe5-b9142cfc08fa</rowguid>
        <ModifiedDate>2001-07-01T00:00:00+01:00</ModifiedDate>
    </Orders>
 </Orders>
</DataSet>
I want XSlt to produce XML like below:

Code:
<?xml version="1.0" standalone="yes"?>
<DataSet> 
    <Orders>
        <SalesOrderID>43659</SalesOrderID>
        <SalesOrderDetailID>1</SalesOrderDetailID>
        <CarrierTrackingNumber>4911-403C-98</CarrierTrackingNumber>        
    </Orders>
    <Orders>
        <SalesOrderID>43659</SalesOrderID>
        <SalesOrderDetailID>2</SalesOrderDetailID>
        <CarrierTrackingNumber>4911-403C-98</CarrierTrackingNumber>        
    </Orders>
 </Orders>
</DataSet>
I am trying to write XSLT like below

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/DataSet"> 
        <xsl:element name="NewDataSet">             
            <xsl:copy-of select="Orders[name()='SalesOrderID' or name()='SalesOrderDetailID' or name()='CarrierTrackingNumber' ]"/>                 
            </xsl:element>
    </xsl:template> 
</xsl:stylesheet>
Based on the node name I want to filter the records.

Please can you correct the xslt what I am doing wrong?

Thanks

Nelly
 
Old August 26th, 2010, 11:14 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

For such tasks start with the identity transformation and then add templates for those elements you want to change:
Code:
<xsl:template match="@* | node()">
  <xsl:copy>
     <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Orders">
  <xsl:copy>
     <xsl:apply-templates select="@* | SalesOrderID | SalesOrderDetailID | CarrierTrackingNumber"/>
   </xsl:copy>
</xsl:template>
You might want to add xsl:output indent="yes" and xsl:strip-space elements="*" for indentation.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 26th, 2010, 11:15 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

name() will return 'Orders' in the predicate you have there as that is the name of the element it is currently in.

You probably want to start with an identity template:

Code:
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
Then handle the DataSet element:

Code:
<xsl:template match="DataSet">
<NewDataSet>
<xsl:apply-templates/>
</NewDataSet>
</xsl:templates>
Finally handle the Orders element:

Code:
<xsl:template match="Orders">
 <Orders>
    <xsl:apply-templates select="SalesOrderID | SalesOrderDetailID | CarrierTrackingNumber"/>
  </Orders>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
nelly78 (August 26th, 2010)
 
Old August 26th, 2010, 11:31 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thank you very much Sam.

It works.
 
Old December 21st, 2010, 08:45 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi All,

Could you please tell me how can I sort the XML result. For example I want resord to be sort by 'SalesOrderID'.

How can I do that?

Thanks
Nelly
 
Old December 21st, 2010, 09:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Add the template rule:

Code:
<xsl:template match="DataSet">
  <xsl:copy>
     <xsl:apply-templates select="Orders">
        <xsl:sort select="SalesOrderID"/>
     </xsl:apply-templates>
   </xsl:copy>
</xsl:template>
When I see questions like this, I always wonder where you looked to find this information before asking. Surely any XSLT reference book has "sorting" in the index? And surely you're not coming to a Wrox forum for help without having a Wrox textbook sitting on your desk?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding xml-html transformation of an xml string using xslt and javascript suprakash444 XSLT 1 January 12th, 2009 01:23 AM
Creating XML doc ; writing string(xml format) into KamalRaturi XML 5 May 28th, 2008 05:51 AM
creating a subset of xml schema using XSLT kapar_p XSLT 8 November 21st, 2006 09:46 AM
creating a structured xml from an unstructured xml geoffgeoffgeoff XSLT 0 August 10th, 2005 02:58 PM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM





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