XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Naveen KSubject: XSLT Problem using Namespace and SchemaLocation
Author: Naveen K
Date: 29 Nov 2005 07:36 PM
Hello Everybody
This is first post...

I am having problems with XML transformation when the XML file has namespace and schemaLocation specified. The transformation just returns an null sets.

Here is the sample code I am using:
XML:
<response-data xmlns="http://w3.org" xmlns:xsi="http://something.com/sample" schemaLocation="http://something.com/sample ex/sample">
<response-data-vals lname="Smith" fname="Allen" dob="19500101"/>
</response-data>

XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:myns="http://something.com/sample ex/sample">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:element name="FirstName">
<xsl:valueof select="myns:response-data/response-data-vals/@fname"/>
</xsl:element>
<xsl:element name="LastName">
<xsl:valueof select="myns:response-data/response-data-vals/@lname"/>
</xsl:element>
<xsl:element name="DOB">
<xsl:valueof select="myns:response-data/response-data-vals/@dob"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

If the Namespace and schemaLocation are not specified in the XML then everything works well.But those values being specified transformation returns null sets.

Any help will be greatly appreciated.

Thanks
Naveen

Postnext
(Deleted User) Subject: XSLT Problem using Namespace and SchemaLocation
Author: (Deleted User)
Date: 29 Nov 2005 10:13 PM
Hi Naveen,
I fixed a couple of problems in your sample.

1) Your input XML file should specify its namespace like this:
<response-data
xmlns="http://something.com/sample ex/sample">
<response-data-vals lname="Smith" fname="Allen" dob="19500101"/>
</response-data>
OR, like this:
<myns:response-data
xmlns:myns="http://something.com/sample ex/sample">
<myns:response-data-vals lname="Smith" fname="Allen" dob="19500101"/>
</myns:response-data>

Both are equivalent.

2) Your .xsl file should be like this:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:myns="http://something.com/sample ex/sample">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:element name="FirstName">
<xsl:value-of select="myns:response-data/myns:response-data-vals/@fname"/>
</xsl:element>
<xsl:element name="LastName">
<xsl:value-of select="myns:response-data/myns:response-data-vals/@lname"/>
</xsl:element>
<xsl:element name="DOB">
<xsl:value-of select="myns:response-data/myns:response-data-vals/@dob"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Note the changes in the xsl:value-of elements.

The actual value for the namespace "http://something.com/sample ex/sample" doesnt matter, as long as it is identical in both files.

Good luck,
Clyde

Postnext
Naveen KSubject: XSLT Problem using Namespace and SchemaLocation
Author: Naveen K
Date: 29 Nov 2005 10:23 PM
Thanks Clyde
I have no control over the input XML, and I wont be able to change any thing on that.
In the xsl translation I am specifying the xmlns as the schemaLocation from the XML i.e., xmlns:myns="schemaLocation" but thats not working.

What needs to be changed on the XSL?

Thanks

Postnext
Ivan PedruzziSubject: XSLT Problem using Namespace and SchemaLocation
Author: Ivan Pedruzzi
Date: 30 Nov 2005 12:00 AM
Naveen,

In your example the default name-space is http://w3.org also the original style-sheet doesn't generate well-formed XML, see the following example


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:myns="http://w3.org">
<xsl:output method="xml"/>
<xsl:template match="/">
<root>
<xsl:element name="FirstName">
<xsl:value-of select="myns:response-data/myns:response-data-vals/@fname"/>
</xsl:element>
<xsl:element name="LastName">
<xsl:value-of select="myns:response-data/myns:response-data-vals/@lname"/>
</xsl:element>
<xsl:element name="DOB">
<xsl:value-of select="myns:response-data/myns:response-data-vals/@dob"/>
</xsl:element>
</root>
</xsl:template>
</xsl:stylesheet>


Hope this helps
Ivan Pedruzzi
Stylus Studio Team

Postnext
Naveen KSubject: XSLT Problem using Namespace and SchemaLocation
Author: Naveen K
Date: 30 Nov 2005 08:49 AM
Thanks Ivan

I had this reformatted to reflect your suggestions and it still doesn't works. Please check this XML and XSL and suggest me if there is something wrong in the XSL.

XML:
<?xml version="1.0" encoding="UTF-8"?>
<response-data xsi:schemaLocation="http://xml-schemas.something.com/sample sample/sampleTest.xsd"
xmlns="http://xml-schemas.something.com/sample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<response-data-vals lname="Smith" fname="Allen" dob="19500101"/>
</response-data-vals>
</response-data>


XSL:
<?xml version='1.0' encoding='UTF-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:java="http://xml.apache.org/xslt/java"
exclude-result-prefixes="java" extension-element-prefixes="java"
xmlns:myns="http://xml-schemas.something.com/sample sample/sampleTest.xsd">

<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<Division>
<PersonalData>
<FirstName>
<xsl:value-of select="myns:response-data/myns:response-data-vals/@fname"/>
</FirstName>
<LastName>
<xsl:value-of select="myns:response-data/myns:response-data-vals/@lname"/>
</LastName>
<DOB>
<xsl:value-of select="myns:response-data/myns:response-data-vals/@dob"/>
</DOB>
</PersonalData>
</Division>
</xsl:template>
</xsl:stylesheet>

Thanks again and I appreciate your response.

Naveen

Postnext
(Deleted User) Subject: XSLT Problem using Namespace and SchemaLocation
Author: (Deleted User)
Date: 30 Nov 2005 09:52 AM
Naveen,

First, your sample input .xml is not well-formed. You are terminating the response-data-vals element 2 times. The xml should be like this:

<?xml version="1.0" encoding="UTF-8"?>
<response-data xsi:schemaLocation="http://xml-schemas.something.com/sample sample/sampleTest.xsd"
xmlns="http://xml-schemas.something.com/sample"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<response-data-vals lname="Smith" fname="Allen" dob="19500101"/>
</response-data>

Second, we need to get the namespaces to match between the two documents.
In the .xml file, you are specifying the namespace with this attribute:
xmlns="http://xml-schemas.something.com/sample"
The other attributes (xsi:schemaLocation and xmlns:xsi) have nothing to do with this problem. You can delete them or leave them it makes no difference.
In the .xsl file, you are specifying the namespace with this attribute:
xmlns:myns="http://xml-schemas.something.com/sample sample/sampleTest.xsd"
The value is not the same, as in the .xml file. Change it to be this:
xmlns:myns="http://xml-schemas.something.com/sample"
and everything will work.

The important thing is that the value of the xmlns= in the .xml file must match the value of the xmlns:myns= in the .xsl file. You can use any string you want, "http://xml-schemas.something.com/sample" is OK. "blah" is also OK, as long as is is the same in both files.

You seem to be trying to use the name of a schema file as the namespace name. That is also OK, but the fact that it is a schema file is not important. You don't specify a schema using the namespace mechanism.

Hope this helps,
Clyde

Postnext
Naveen KSubject: XSLT Problem using Namespace and SchemaLocation
Author: Naveen K
Date: 01 Dec 2005 10:24 AM
Originally Posted: 01 Dec 2005 10:25 AM
Thanks Clyde
That did worked. The termination of the response-data-vals element 2 times was a typo.

Here is another problem, the input XML is inconsistent. Sometimes I recieve xsi:schemaLocation,xmlns and xmlns:xsi and sometimes I just recieve the xsi:schemaLocation.
When I just recieve the xsi:schemaLocation the translation fails. Any idea what needs to be changed in the xsl to handle this problem?

<?xml version="1.0" encoding="UTF-8"?>
<response-data xsi:schemaLocation="http://xml-schemas.something.com/sample sample/sampleTest.xsd">
<response-data-vals lname="Smith" fname="Allen" dob="19500101"/>
</response-data>

Any help will be great.
Thanks
Naveen

Postnext
(Deleted User) Subject: XSLT Problem using Namespace and SchemaLocation
Author: (Deleted User)
Date: 01 Dec 2005 01:17 PM
Naveen,
If there is too much variation in the incoming data, then you are going to run into trouble. If the only variation is that sometimes the data has no namespace, and sometimes it has the namespace "http://xml-schemas.something.com/sample"
then the attached program works. It first tries to find elements in the namespace, then it tries to find elements without a namespace.
There are many other ways to do the same thing, with <xsl:if> or <xsl:choose>

Clyde


Documenttest2.xsl

Postnext
(Deleted User) Subject: XSLT Problem using Namespace and SchemaLocation
Author: (Deleted User)
Date: 01 Dec 2005 01:37 PM
Naveen,
The last sample of input XML which you supplied was also not valid XML.
You won't succeed at getting XSLT to work if your input XML is not valid.

You need to solve that problem first, then work on getting the XSLT to work.

Clyde

Postnext
Naveen KSubject: XSLT Problem using Namespace and SchemaLocation
Author: Naveen K
Date: 01 Dec 2005 01:41 PM
Thanks Clyde
That can be done as a work around.

But is it valid to specify xsi:schemaLocation without having a xmlns or/and xmlns:xsi. My question of whether it is valid arises because I cannot open this file in Internet Explorer and it gives me an error as
"Reference to undeclared namespace prefix: 'xsi'. Error processing resource 'file:///C:/output.xml'. Line 1, Position 148".

<?xml version="1.0" encoding="UTF-8"?>
<response xsi:schemaLocation="http://xml-schemas.something.com/test/samples test/samples">
<response-header status="OK"/>
<response-record CODE="500" NUM="999999" DATE="2005-11-29"> </response-record>
</response>

I appreciate your response and time.

Thanks
Naveen

Postnext
(Deleted User) Subject: XSLT Problem using Namespace and SchemaLocation
Author: (Deleted User)
Date: 01 Dec 2005 02:57 PM
Naveen,

It is not valid to have xsi:.... without xmlns:xsi=....

The sample of XML you sent won't work with IE, Stylus Studio or any other XML processor.

So if you have xsi:schemaLocation= then you must also have xmlns:xsi=

But, the xmlns="...." is totally separate. It has no connection with the other 2. It doesn't care if the other 2 exist and ignores whatever values they may or may not have.

Clyde

Posttop
Naveen KSubject: XSLT Problem using Namespace and SchemaLocation
Author: Naveen K
Date: 01 Dec 2005 03:25 PM
Thanks Clyde
Now it definitely makes some sense for me as why my translation is failing.

Thanks again for all the help.

Naveen

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.