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 May 16th, 2011, 06:36 AM
Registered User
 
Join Date: May 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to pass node attribute?

Hi Experts,

My XSLT is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="urn:sap-com:document:sap:rfc:functions" xmlns:ns1="http://test.com/XSLT_POC">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="/ns0:YTEST_SUM.Response">
<ns1:YCUST_OUTPUT><OUT>
<xsl:value-of select="SUM"/>
</OUT>
</ns1:YCUST_OUTPUT>
</xsl:template>
</xsl:stylesheet>

My input XML is:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:YTEST_SUM.Response xmlns:ns0="urn:sap-com:document:sap:rfc:functions">
<SUM ID="S01">12</SUM>
</ns0:YTEST_SUM.Response>


when I use the XSLT for this input XML then the output is:

<?xml version="1.0" encoding="utf-8"?>
<ns1:YCUST_OUTPUT xmlns:ns1="http://test.com/XSLT_POC">
<OUT>12</OUT>
</ns1:YCUST_OUTPUT>

But I want it like this:

<?xml version="1.0" encoding="utf-8"?>
<ns1:YCUST_OUTPUT xmlns:ns1="http://test.com/XSLT_POC" xmlns:ns0="urn:sap-com:document:sap:rfc:functions">
<OUT ID = "S01">12</OUT>
</ns1:YCUST_OUTPUT>

I want the node attribute (ID = "S01") should also be propagated to the output.

How can I achieve this? What changes I need to do in my XSLT?

Please help!

Thanks
Gopal
 
Old May 16th, 2011, 06:43 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:template match="/ns0:YTEST_SUM.Response">
<ns1:YCUST_OUTPUT><OUT ID="{SUM/@ID}">
<xsl:value-of select="SUM"/>
</OUT>
</ns1:YCUST_OUTPUT>
</xsl:template>
might suffice though writing a template for the SUM element with e.g.
Code:
<xsl:template match="SUM">
  <OUT ID="{@ID}">
    <xsl:value-of select="."/>
  </OUT>
</xsl:template>
and then applying that with e.g.
Code:
<xsl:template match="/ns0:YTEST_SUM.Response">
<ns1:YCUST_OUTPUT>
  <xsl:apply-templates/>
</ns1:YCUST_OUTPUT>
</xsl:template>
is cleaner and easier to extend and enhance.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to select a branch of an xml file if it contains a node with a certain attribute? dbsHarry XSLT 5 March 17th, 2009 09:38 AM
Get a node value using attribute name IronStar XSLT 4 May 29th, 2008 04:28 PM
Convert attribute to node-set using xalan Coolcampers XSLT 1 May 3rd, 2008 06:06 AM
to get Distinct Node depending on attribute value chaitanyaXML XML 5 March 29th, 2005 10:26 AM
How can I get the element node of an attribute cdias XSLT 2 March 15th, 2004 10:34 AM





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