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 October 25th, 2011, 05:45 AM
Authorized User
 
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Default Help getting started

Guys,
I am quite new to XSLT and am trying to write a transformation from one form of xml to another.
see below for example of input xml.

<?xml version="1.0" encoding="utf-8" ?>
- <exportToOMSNotification com:msgId="ssb.bos.george.-1b0c8320:1211b97db0e:-7fbc" com:timeStamp="2011-05-07T15:03:52Z" xmlns="http://www.globallink.com/schemas/fxcng/glml/v67/extra" xmlns:com="http://www.globallink.com/schemas/fxcng/glml/v67/common" xmlns:mar="http://www.globallink.com/schemas/fxcng/glml/v67/market">
- <com:routingInfo>
<com:from com:group="bos" com:institution="ssb" com:user="bos.ssb.george" com:side="SELL" />
<com:to com:group="" com:institution="" com:user="" com:side="BUY" />
</com:routingInfo>
- <marketTrade mar:type="MarketRegular" com:id="3" com:userSessionID="SID-107de848:120b5fe70e6:-8000">
<com:date>2011-05-07T14:48:40Z</com:date>
<com:finishedTime>2011-05-07T14:52:49Z</com:finishedTime>
- <com:tradeAssignment>
<com:buySideAssignedTo com:institution="cust1" com:user="bos.cust1.george" />
<com:sellSideAssignedTo com:institution="ssb" />




However, When I write and run my xml it does not seem to be working:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match = "/">
<xml version='1.0' encoding="utf-8">
<xsl:element name='myXml'>

<xsl:apply-templates select ="." mode ="exportToOMSNotification_Template"/>
</xsl:element>
</xml>

</xsl:template>


<xsl:template match ="exportToOMSNotification" mode ="exportToOMSNotification_Template">
<xsl:element name='myelement'>

<xsl:variable name ='myVar'>
<xsl:value-of select ='.'/>
</xsl:variable>

<xsl:copy-of select ='$myVar'/>-->
</xsl:element>

</xsl:template>


</xsl:stylesheet>

In that the data written to output xml file is just random bits of data from the input xml.
please can someone tell me what i am doing wrong.
thanks
 
Old October 25th, 2011, 05:59 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

See http://www.dpawson.co.uk/xsl/sect2/N...tml#d7038e1015
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 25th, 2011, 06:10 AM
Authorized User
 
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi.. am not sure i follow?
 
Old October 25th, 2011, 06:23 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well your sample has elements in a default namespace (exportToOMSNotification xmlns="http://www.globallink.com/schemas/fxcng/glml/v67/extra") and the article in the FAQ I linked to explains how to deal with elements in a default namespace in XSLT 1.0.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 25th, 2011, 08:38 AM
Authorized User
 
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Martin,

I have followed the templates on the website and it still does not work....
Please can you show me where i am going wrong. I really appreciate yuor help on this.. as no-one in my office does this and am a little lost..


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns="http://www.globallink.com/schemas/fxcng/glml/v67/extra"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:com="http://www.globallink.com/schemas/fxcng/glml/v67/common"
xmlns:mar="http://www.globallink.com/schemas/fxcng/glml/v67/market">

<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match = "/">
<xml version='1.0' encoding="utf-8">
<xsl:element name='myXml'>

<xsl:apply-templates select ="." mode ="exportToOMSNotification_Template"/>
</xsl:element>
</xml>

</xsl:template>


<xsl:template match ="exportToOMSNotification" mode ="exportToOMSNotification_Template">
<xsl:element name='myelement'>

<xsl:variable name ='myVar'>
<xsl:value-of select ='.'/>
</xsl:variable>

<xsl:copy-of select ='$myVar'/>-->
</xsl:element>

</xsl:template>


</xsl:stylesheet>


retgards
 
Old October 25th, 2011, 08:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

In XSLT 1.0 you cannot match against elements in a namespace by redefining the 'null' namespace (i.e. xmlns="http://www.globallink.com/schemas/fxcng/glml/v67/extra") as XPath does not pick it up and it is XPath that is used for the pattern matching.

You need to define the above namespace with a prefix and then use that prefix in XPath.

xmlns:glml="http://www.globallink.com/schemas/fxcng/glml/v67/extra"

<xsl:template match="glml:exportToOMSNotification" ... etc
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old October 25th, 2011, 08:46 AM
Authorized User
 
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Smile

BEAUTIFUL....

THANKS SO MUCH GUYS I REALLY APPRECIATE YOUR HELP HERE.......
DERINKS ON ME
 
Old October 25th, 2011, 09:16 AM
Authorized User
 
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Why does it miss out the 'marketTrade' template then spew out loads of data into the output file...


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:glml="http://www.globallink.com/schemas/fxcng/glml/v67/extra"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:com="http://www.globallink.com/schemas/fxcng/glml/v67/common"
xmlns:mar="http://www.globallink.com/schemas/fxcng/glml/v67/market">

<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match = "/">
<xml version='1.0' encoding="utf-8">
<xsl:element name='myXml'>

<xsl:apply-templates select ="." mode ="exportToOMSNotification_Template"/>
</xsl:element>
</xml>

</xsl:template>


<xsl:template match ="glml:exportToOMSNotification" mode ="exportToOMSNotification_Template">
<xsl:element name='exportToOMSNotification'>
<xsl:comment>THIS IS A COMMENT</xsl:comment>

<xsl:apply-templates select ="." mode ="routingInfo_Template"/>
<xsl:apply-templates select ="." mode ="marketTrade_Template"/>
</xsl:element>

</xsl:template>



<xsl:template match ="com:routingInfo" mode ="routingInfo_Template">
<xsl:element name='routingInfo'>
<xsl:comment>THIS IS A COMMENT2</xsl:comment>

</xsl:element>
</xsl:template>


<xsl:template match ="marketTrade" mode ="marketTrade_Template">
<xsl:element name='marketTrade'>
<xsl:comment>THIS IS A COMMENT3</xsl:comment>

</xsl:element>
</xsl:template>

</xsl:stylesheet>


cheers
 
Old October 25th, 2011, 09:33 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

That's the same problem (element in a default namespace) with the same solution (define and use prefix) as before:
Code:
<xsl:template match="gml:marketTrade">
Also please try to mark up code samples with http://p2p.wrox.com/misc.php?do=bbcode#code.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 25th, 2011, 09:53 AM
Authorized User
 
Join Date: Oct 2011
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Default

thanmk you... but why do i get a dump of data inn the middle of my output xml file.....

Am new to this so its all a learning curve.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Started R1chardK Perl 2 August 19th, 2009 11:22 PM
Getting Started... aweTastic SharePoint Development 2 April 10th, 2009 04:42 AM
Getting Started st3ve Javascript How-To 1 February 3rd, 2005 08:49 PM





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