473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 6654
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

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

Similar topics

1
by: Chris Bedford | last post by:
Hi: this question has been driving me crazy. would be grateful for any help. I am writing a version of the identity transformation to filter some documents (they happen to be wsdl docs)....
1
by: jorgen.wahlund | last post by:
Hi I have problems with transforming a xml document with Xalan and javax.xml.Transformer. Following xsl is used: <xsl:stylesheet version='1.0'...
4
by: Gismo | last post by:
I have got file raport.rld which is an XML file generated by MS Reporting Services. The problem is: in this file are tags from two different namespaces ...
4
by: David S. Alexander | last post by:
I am trying to transform XML to XML using an XSLT in C#, but the root node of my XML is not being matched by the XSLT if it has an xmlns attribute. Am I handling my namespaces incorrectly? My C#...
1
by: Rajesh | last post by:
I am trying to pass the namespace, which is in my Original Message, to a Java method for further processing. In the original message the xmlns:xenc namespace is present. I make a call to Java...
0
by: jts2077 | last post by:
I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as: <store ...
3
by: Gary Stephenson | last post by:
I am endeavouring to use .NET v2.0 XML facilities to "roundtrip" reading and writing XML documents - i.e. to end up with _exactly_ what I started with, but I can't seem to figure out how to get an...
3
by: Keith Patrick | last post by:
I'm doing some document merging where I want to bring in an XmlDocument and import its document element into another document deeper in its tree. However, when serializing my underlying objects,...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
8
by: nwbabcock | last post by:
I'm using XmlSerializer in C# to deserialize an xml into classes. First I generated the code from the schema using xsd.exe The error I recieve is: Exception:There is an error in XML document (2,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.