472,119 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

XSL xmlns problem

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
May 15 '09 #1
11 6585
jkmyoung
2,057 Expert 2GB
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"
May 15 '09 #2
eb621
6
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>
May 21 '09 #3
Dormilich
8,658 Expert Mod 8TB
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.
May 21 '09 #4
jkmyoung
2,057 Expert 2GB
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.
May 21 '09 #5
eb621
6
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>
May 22 '09 #6
jkmyoung
2,057 Expert 2GB
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).
May 25 '09 #7
eb621
6
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
May 26 '09 #8
Dormilich
8,658 Expert Mod 8TB
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">
May 26 '09 #9
eb621
6
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>
May 26 '09 #10
Dormilich
8,658 Expert Mod 8TB
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.
May 26 '09 #11
eb621
6
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...
May 27 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by jorgen.wahlund | last post: by
4 posts views Thread by Gismo | last post: by
3 posts views Thread by Gary Stephenson | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.