XSL xmlns problem 
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
| 
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"
| 
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 - <?xml version="1.0" encoding="UTF-8"?>
-
<?xml-stylesheet type="text/xsl" href="main-transform.xsl"?>
-
<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">
-
<title>Companies Act 1985</title>
-
<paragraph role="annotation">
-
<title>Note</title>
-
<para>For the registrar's index of company names see s. 714</para>
-
</paragraph>
-
</document>
XSL - <?xml version="1.0" encoding="UTF-8"?>
-
<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">
-
-
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
-
<xsl:strip-space elements="*"/> <!-- for xalan on pc -->
-
-
<xsl:template match="/">
-
<xsl:apply-templates/>
-
</xsl:template>
-
-
<xsl:template match="title">
-
<xsl:text><UN+></xsl:text>
-
<xsl:apply-templates/>
-
<xsl:text><UN-><CR>
-
</xsl:text>
-
</xsl:template>
-
-
<xsl:template match="paragraph">
-
<xsl:text><IT+></xsl:text>
-
<xsl:apply-templates/>
-
<xsl:text><IT-><CR>
-
</xsl:text>
-
</xsl:template>
-
-
<xsl:template match="para">
-
<xsl:apply-templates/>
-
<xsl:text><CR>
-
</xsl:text>
-
</xsl:template>
-
-
</xsl:stylesheet>
Last edited by Dormilich; May 21st, 2009 at 08:28 PM.
Reason: please use [code] tags when posting code
| 
May 21st, 2009, 08:34 PM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9 | | | re: XSL xmlns problem - <xsl:template match="legis:title">
-
<xsl:text><UN+></xsl:text>
-
<xsl:apply-templates/>
-
<xsl:text><UN-><CR>
-
</xsl:text>
-
</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 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.
| 
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... - <?xml version="1.0" encoding="UTF-8"?>
-
<?xml-stylesheet type="text/xsl" href="main-transform.xsl"?>
-
<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">
-
<title>Companies Act 1985</title>
-
<paragraph role="annotation">
-
<title>Note</title>
-
<para>For the registrar's index of company names see s. 714</para>
-
</paragraph>
-
</document>
- <?xml version="1.0" encoding="UTF-8"?>
-
<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">
-
-
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
-
<xsl:strip-space elements="*"/> <!-- for xalan on pc -->
-
-
<xsl:template match="/">
-
<xsl:apply-templates/>
-
</xsl:template>
-
-
<xsl:template match="z:title">
-
<xsl:text><UN+></xsl:text>
-
<xsl:apply-templates/>
-
<xsl:text><UN-><CR>
-
</xsl:text>
-
</xsl:template>
-
-
<xsl:template match="z:paragraph">
-
<xsl:text><IT+></xsl:text>
-
<xsl:apply-templates/>
-
<xsl:text><IT-><CR>
-
</xsl:text>
-
</xsl:template>
-
-
<xsl:template match="z:para">
-
<xsl:apply-templates/>
-
<xsl:text><CR>
-
</xsl:text>
-
</xsl:template>
-
-
</xsl:stylesheet>
| 
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).
| 
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
| 
May 26th, 2009, 03:47 PM
|  | 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 - <namepace:element>
-
<!-- or -->
-
<element xmlns="namespace-uri">
| 
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... - <xref xmlns:xlink="http://www.w3.org/TR/xlink/" xlink:type="simple" link:href="p4600B/>
-
-
<graphic xmlns:ns1=http://xml.dummy.com/schema/lcr ns1:id="I3393df708bb611dcbdd2b4d3d31e5c5e"><caption>transport A to B</caption></graphic>
| 
May 26th, 2009, 05:25 PM
|  | 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.
| 
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...
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|