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 19th, 2010, 04:11 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default defining namespaces for source and target

Hello,

I have written a xslt file that transforms a xml file based on a older schema to the newer schema, well sort of. everything was working correctly until I realized that in the xslt file I was using the older schema in the <xsl:stylesheet> node. This namespce with other namespaces were copied to my target xml file, which was incorrect, so I changed it to the new schema and now nothing is working?? I don't get the nodes just the value of nodes. I don't understand why is this happening. I thought that when I write a xslt, the parser will read and match the nodes in the source file with the templates I have created and based on the template create an output in the target file, so why does this happen the source file hasn'e changed and neither the templates? is there any way to fix this by saying that the source is based on this schema and the target is based on the other schema?

regards,
es
 
Old October 19th, 2010, 04:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Usually when converting from one namespace to another you will see both namespaces defined in the xsl:stylesheet element.

But without seeing some of your actual code I don't really want to comment further.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old October 19th, 2010, 04:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Changing the namespace in the root element of your document seems like a very small change, but you need to realise that it changes everything. As far as the stylesheet is concerned, it's like changing all the element names from upper case to lower case - nothing will match any more. If the stylesheet is designed to work on names in a particular namespace, then it won't work on names in a different namespace.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
ehsansad (October 19th, 2010)
 
Old October 19th, 2010, 05:11 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

thanks for both comments,

I've just realized I the two schemas are using the same namespace id,
Code:
xmlns:be=...
so when I changed it to the new one, it was infact trying to read the source based on the new one. Now my question is since I'm not allowed to change the schemas is there any way to say that in the target file use this url and not the otherone.

ehsan
 
Old October 19th, 2010, 05:15 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

No, that's not how it works unfortunately. You'd need to define two namespaces, and select everything in your input XML using one, and output everything using the other. E.g:

Code:
<xsl:stylesheet xmlns:old="http://old.com" xmlns:new="http://new.com" ...>

<xsl:template match="old:element">
  <new:element>...</new:element>
</xsl:template>

...
If you provided us with examples of your code and xml we could probably help you much better.
__________________
/- 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:
ehsansad (October 19th, 2010)
 
Old October 19th, 2010, 06:59 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

out of curiosity, based on the discussion above if I do something like this
Code:
       <xsl:element name="benew:forretningsadress">
          <xsl:copy-of select="be:adresse"/>
          <xsl:element name="benew:kommunennummer"/>
        </xsl:element>
is it possible using xslt to change the prefix "be" in be:adresse to "benew" after the <xsl:copy-of> ?

ehsan
 
Old October 19th, 2010, 07:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

No. xsl:copy-of copies an xml element - an xml element is defined by its fully qualified name (i.e. the local name and the namespace), so changing the namespace it becomes a completely different element - hence 'copy' can't be used.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
transform target file from 2 source files metinhoclam XSLT 7 September 17th, 2010 07:39 PM
Preserve prefix namespaces from source document Marcela Sobrinho XSLT 3 April 17th, 2009 09:28 AM
Copying Branches From a Source to a Target chernevik XSLT 7 July 19th, 2008 08:46 AM
Converting Source Xml into Target Xml Using XSL. alapati.sasi XSLT 3 May 14th, 2007 10:54 AM
Creating Target Elements with Namespaces alapati.sasi XSLT 1 April 11th, 2007 05:31 AM





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