Connecting Tech Pros Worldwide Help | Site Map

XSL xmlns problem

  #1  
Old May 15th, 2009, 04:41 PM
Newbie
 
Join Date: May 2009
Posts: 6
Hi,

I have an xsl transform to convert xml into another (non-standard) data structure. All is fine until I have an xml file with xmlns atts and then all I get is the element contents output with none of the tagging that I have written in. Remove the xmlns atts from the xml and all is fine. I’ve tried adding in the xmlns from the xml into the xsl:stylesheet element of the xsl but get the same result. This is the same run on xmlspy and with xalan.

Can anyone point me in the direction of where I’m going wrong?

Thanks
E
  #2  
Old May 15th, 2009, 08:00 PM
Moderator
 
Join Date: Mar 2006
Posts: 1,103

re: XSL xmlns problem


You need to edit your xpaths to use the namespace.

eg, say your xpath is "/root/somenode/leaf/@id"
declare your namespace like xmlns:sns="www.google.ca"
your xpath then becomes: "/sns:root/sns:somenode/sns:leaf/@id"
  #3  
Old May 21st, 2009, 04:34 PM
Newbie
 
Join Date: May 2009
Posts: 6

re: XSL xmlns problem


Hi,

I may well be understanding this incorrectly... please see the dummy examples below

I though that this - xmlns="http://xml.dummy.com/schema/analytical"- would give some sort of default

If you run the xml as is through the xsl you don't get any of the (archaic) tagging in the xsl:text elements output but remove the xmlns atts from the document node of the xml and you do...

Still consfused I'm afraid.

Thanks for your help on this...

xml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?xml-stylesheet type="text/xsl" href="main-transform.xsl"?>
  3. <document xmlns:legis="http://xml.dummy.com/schema/legislation/act" xmlns:xlink="http://www.w3.org/TR/xlink/" xmlns:a="http://xml.dummy.com/schema/amendments" xmlns="http://xml.dummy.com/schema/analytical">
  4.     <title>Companies Act 1985</title>
  5.     <paragraph role="annotation">
  6.         <title>Note</title>
  7.         <para>For the registrar's index of company names see s. 714</para>
  8.     </paragraph>
  9. </document>
XSL
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:legis="http://xml.dummy.com/schema/legislation/act" xmlns:xlink="http://www.w3.org/TR/xlink/" xmlns:a="http://xml.dummy.com/schema/amendments" xmlns="http://xml.dummy.com/schema/analytical">
  3.  
  4.     <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
  5.     <xsl:strip-space elements="*"/> <!-- for xalan on pc -->
  6.  
  7.     <xsl:template match="/">
  8.         <xsl:apply-templates/>
  9.     </xsl:template>
  10.  
  11.     <xsl:template match="title">
  12.         <xsl:text>&lt;UN+&gt;</xsl:text>
  13.         <xsl:apply-templates/>
  14.         <xsl:text>&lt;UN-&gt;&lt;CR&gt;
  15. </xsl:text>
  16.     </xsl:template>
  17.  
  18.     <xsl:template match="paragraph">
  19.         <xsl:text>&lt;IT+&gt;</xsl:text>
  20.         <xsl:apply-templates/>    
  21.         <xsl:text>&lt;IT-&gt;&lt;CR&gt;
  22. </xsl:text>
  23.     </xsl:template>
  24.  
  25.     <xsl:template match="para">
  26.         <xsl:apply-templates/>    
  27.         <xsl:text>&lt;CR&gt;
  28. </xsl:text>
  29.     </xsl:template>
  30.  
  31. </xsl:stylesheet>

Last edited by Dormilich; May 21st, 2009 at 08:28 PM. Reason: please use [code] tags when posting code
  #4  
Old May 21st, 2009, 08:34 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: XSL xmlns problem


Expand|Select|Wrap|Line Numbers
  1.     <xsl:template match="legis:title">
  2.         <xsl:text>&lt;UN+&gt;</xsl:text>
  3.         <xsl:apply-templates/>
  4.         <xsl:text>&lt;UN-&gt;&lt;CR&gt;
  5. </xsl:text>
  6.     </xsl:template>
this is what jkmyoung was talking about. in any XPath (i.e. also in the match attribute) the appropriate namespace has to be added. otherwise the default namespace ("" (empty namespace)) is used and you get no match.
  #5  
Old May 21st, 2009, 08:34 PM
Moderator
 
Join Date: Mar 2006
Posts: 1,103

re: XSL xmlns problem


Just declaring it there is not enough. The xsl still looks for these elements with the default empty namespace. You need to declare the namespace with a prefix, eg and then add the prefix before the element name in each of the templates:

eg, like:
xmlns:rr="http://xml.dummy.com/schema/analytical"

<xsl:template match="rr:title">

It is confusing, and I remember trying to do it the same way when I started.
  #6  
Old May 22nd, 2009, 10:09 AM
Newbie
 
Join Date: May 2009
Posts: 6

re: XSL xmlns problem


I tried this (changing the default name space to a "z" namespace) and still have the problem (below)

My understanding is that although it's convention to use the schema location for the name space it doesn't actually go off and look at the schema. I have a merry band of schemas that make the rules (64 to date) but this should be irrelevant?! In any case the dummy code I've given below is too simple for this to be an issue.

Any further thoughts? - thanks for your input...

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?xml-stylesheet type="text/xsl" href="main-transform.xsl"?>
  3. <document xmlns:legis="http://xml.dummy.com/schema/legislation/act" xmlns:xlink="http://www.w3.org/TR/xlink/" xmlns:a="http://xml.dummy.com/schema/amendments" xmlns:z="http://xml.dummy.com/schema/analytical">
  4.     <title>Companies Act 1985</title>
  5.     <paragraph role="annotation">
  6.         <title>Note</title>
  7.         <para>For the registrar's index of company names see s. 714</para>
  8.     </paragraph>
  9. </document>
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:legis="http://xml.dummy.com/schema/legislation/act" xmlns:xlink="http://www.w3.org/TR/xlink/" xmlns:a="http://xml.dummy.com/schema/amendments" xmlns:z="http://xml.dummy.com/schema/analytical">
  3.  
  4.     <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
  5.     <xsl:strip-space elements="*"/> <!-- for xalan on pc -->
  6.  
  7.     <xsl:template match="/">
  8.         <xsl:apply-templates/>
  9.     </xsl:template>
  10.  
  11.     <xsl:template match="z:title">
  12.         <xsl:text>&lt;UN+&gt;</xsl:text>
  13.         <xsl:apply-templates/>
  14.         <xsl:text>&lt;UN-&gt;&lt;CR&gt;
  15. </xsl:text>
  16.     </xsl:template>
  17.  
  18.     <xsl:template match="z:paragraph">
  19.         <xsl:text>&lt;IT+&gt;</xsl:text>
  20.         <xsl:apply-templates/>    
  21.         <xsl:text>&lt;IT-&gt;&lt;CR&gt;
  22. </xsl:text>
  23.     </xsl:template>
  24.  
  25.     <xsl:template match="z:para">
  26.         <xsl:apply-templates/>    
  27.         <xsl:text>&lt;CR&gt;
  28. </xsl:text>
  29.     </xsl:template>
  30.  
  31. </xsl:stylesheet>
  #7  
Old May 25th, 2009, 04:04 PM
Moderator
 
Join Date: Mar 2006
Posts: 1,103

re: XSL xmlns problem


Lol, by changing the namespace prefix in your source xml, you've moved all those elements back into the default namespace, (because there is no element with just xmlns="something.com"

Easiest change is to change it to xmlns="http://xml.dummy.com/schema/analytical" in ONLY your source xml. Another possible way to fix it is to prefix z: before each of the elements in your source xml (waste of time).
  #8  
Old May 26th, 2009, 03:42 PM
Newbie
 
Join Date: May 2009
Posts: 6

re: XSL xmlns problem


Hi again,

I tried that but that didn't work ...! I've played around with a few combos of what I had (just for that namespace) and it works if I remove it completely from the xml. It’s also OK if change it to xmlns:z in the xml and just xmlns in the xsl. It doesn’t seem to mind one way or the other what I have in the xsl.

This means that I’ll have to do a bit or pre-processing to remove this from the xml, which I was hoping to avoid. Are there any other alternatives? Will I have any potential problems of I do remove this?

Thanks for this
  #9  
Old May 26th, 2009, 03:47 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: XSL xmlns problem


do you use any of the namespaces in your XML (despite defining them)?

like
Expand|Select|Wrap|Line Numbers
  1. <namepace:element>
  2. <!-- or -->
  3. <element xmlns="namespace-uri">
  #10  
Old May 26th, 2009, 05:05 PM
Newbie
 
Join Date: May 2009
Posts: 6

re: XSL xmlns problem


It's early days and the datamodel is still being written and I have precious little sample data to go on but I'd say yes - things like the samples below are what I'm seeing...

Expand|Select|Wrap|Line Numbers
  1. <xref  xmlns:xlink="http://www.w3.org/TR/xlink/" xlink:type="simple" link:href="p4600B/>
  2.  
  3. <graphic xmlns:ns1=http://xml.dummy.com/schema/lcr ns1:id="I3393df708bb611dcbdd2b4d3d31e5c5e"><caption>transport A to B</caption></graphic>
  #11  
Old May 26th, 2009, 05:25 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: XSL xmlns problem


from the code I see, you don't need the namespace when you access the elements (except when you have a global namespace), but you need it when accessing (some of) the attributes.
  #12  
Old May 27th, 2009, 08:54 AM
Newbie
 
Join Date: May 2009
Posts: 6

re: XSL xmlns problem


I think that when I get a fulll set of data namespaces are going to be quite heavily used so I do need them.

I guess I'll just remove the one that causes problems from the XML as it comes in...

Thanks both of you for your advice on this...
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL namespace problem Sandy answers 3 December 21st, 2005 12:05 AM
XSL xmlns usage problems during transform jorgen.wahlund@gmail.com answers 1 October 26th, 2005 12:25 PM
Has anyone solved the problem of lists in WordML (Word 2003)? Clifford W. Racz answers 5 July 20th, 2005 08:20 AM
Problem with xsl:call-template Remi COCULA answers 2 July 20th, 2005 07:40 AM