XSLT and XSD

M

Mark Constant

I posted this on topxml.com and nobody has responded so I was hoping
somebody could help me here. I am following some tutorials in VS.NET.
I created a XSD schema file and then from there created an XML file.
Everything works okay and my data is validated against the schema. Now
when I try to display my xml using xsl it won't work unless I get rid
of the xmlns property of the xml file that points to my schema. Why
would I need to get rid of that property for the xml to display?
 
F

FC

I have a slightly different problem instead and I can't find the way out.
I am not using an explicit default namespace, but I am hitting the famous
problem of the unwanted namespace declarations inside result tree elements:

Take the samples at TopXml and change them slightly:


<?xml version="1.0" encoding="UTF-8"?>

<requestHierarchySelectResult resultName=""
xmlns:cs="http://www.customsolids.com">

<request>

<created_dt>05/05/2000 00:00:00</created_dt>

<created_tm>01/01/1900 14:02:46</created_tm>

<cs:request_id>

<my_value>12345</my_value>

</cs:request_id>

</request>

</requestHierarchySelectResult>



<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:cs="http://www.customsolids.com"

version="1.0"

exclude-result-prefixes="cs">

<xsl:eek:utput indent="yes"/>

<xsl:template match="/">

<out>

<xsl:apply-templates select="//cs:request_id/my_value"/>

</out>

</xsl:template>

<xsl:template match="*|@*|node()"/>

<xsl:template match="my_value">

<xsl:copy>

<xsl:value-of select="."/>

</xsl:copy>

</xsl:template>

</xsl:stylesheet>



The result is:

<?xml version="1.0" encoding="UTF-8"?>

<out>

<my_value xmlns:cs="http://www.customsolids.com">12345</my_value>

</out>



My question is, how to get rid of the xmlns:cs namespace declararion inside
<my_value>?

Bye,

Flavio
 
D

Dimitre Novatchev

FC said:
I have a slightly different problem instead and I can't find the way out.
I am not using an explicit default namespace, but I am hitting the famous
problem of the unwanted namespace declarations inside result tree elements:

Take the samples at TopXml and change them slightly:


<?xml version="1.0" encoding="UTF-8"?>

<requestHierarchySelectResult resultName=""
xmlns:cs="http://www.customsolids.com">

<request>

<created_dt>05/05/2000 00:00:00</created_dt>

<created_tm>01/01/1900 14:02:46</created_tm>

<cs:request_id>

<my_value>12345</my_value>

</cs:request_id>

</request>

</requestHierarchySelectResult>



<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:cs="http://www.customsolids.com"

version="1.0"

exclude-result-prefixes="cs">

<xsl:eek:utput indent="yes"/>

<xsl:template match="/">

<out>

<xsl:apply-templates select="//cs:request_id/my_value"/>

</out>

</xsl:template>

<xsl:template match="*|@*|node()"/>

<xsl:template match="my_value">

<xsl:copy>

<xsl:value-of select="."/>

</xsl:copy>

</xsl:template>

</xsl:stylesheet>



The result is:

<?xml version="1.0" encoding="UTF-8"?>

<out>

<my_value xmlns:cs="http://www.customsolids.com">12345</my_value>

</out>



My question is, how to get rid of the xmlns:cs namespace declararion inside
<my_value>?


The short answer is: You get what you specified -- copying an element copies
all its namespace nodes. This element is not a literal-result-element, so
exclude-result-prefixes does not apply to it.

Also, the meaning of copying is to copy the node as is with no change. This
means that all namespace nodes of the node must be copied.

To achieve what you want use:

<xsl:template match="my_value">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
F

FC

Dimitre Novatchev said:
The short answer is: You get what you specified -- copying an element copies
all its namespace nodes. This element is not a literal-result-element, so
exclude-result-prefixes does not apply to it.

Also, the meaning of copying is to copy the node as is with no change. This
means that all namespace nodes of the node must be copied.

To achieve what you want use:

<xsl:template match="my_value">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Thanks Dimitre!
What makes me feel so sad is that I've been using Xpath inline expressions
all over my transformations and my brain went blank in this case.
I mean, it's so obvious I can solve it by replacing each and every element
with its cleansed counterpart by means of name().
And I can't even take a vacation, it's still 9 months ahead...

Bye,
Flavio
 
M

Mark Constant

Dimitre Novatchev said:
This is a very FAQ. Read for example:

None of my XPath select statements will work going against an XML file with
a default namespace. Help!
by Mark Bosley



at: http://www.topxml.com/people/bosley/defaultns.asp



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Thank you for your help. I got it so the elements display on a screen
but now the HTML doesn't work. Here is what I have so far. Sorry if
this looks ugly but I am new to this.


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lc="http://mark/DevelopmentWebsite">
<xsl:template match="lc">
<h2>My Game Collection</h2>
<table border="1">
<xsl:for-each select="lc:Entertainment/GameList">
<tr>
<th colspan="2"><xsl:value-of select="lc:Title"/></th>
</tr>
<tr>
<td>
<xsl:element name="lc:IMG">
<xsl:attribute name="lc:SRC">
<xsl:value-of select="lc:Image"/>
</xsl:attribute>
</xsl:element>
</td>
<td><xsl:value-of select="lc:Description"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
 
D

Dimitre Novatchev

Thank you for your help. I got it so the elements display on a screen
but now the HTML doesn't work. Here is what I have so far. Sorry if
this looks ugly but I am new to this.


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lc="http://mark/DevelopmentWebsite">
<xsl:template match="lc">
<h2>My Game Collection</h2>
<table border="1">
<xsl:for-each select="lc:Entertainment/GameList">
<tr>
<th colspan="2"><xsl:value-of select="lc:Title"/></th>
</tr>
<tr>
<td>
<xsl:element name="lc:IMG">
<xsl:attribute name="lc:SRC">
<xsl:value-of select="lc:Image"/>
</xsl:attribute>
</xsl:element>
</td>
<td><xsl:value-of select="lc:Description"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>


Unless somebody is a clairevoyant (and also knows xslt) you're probably not
going to get any help.

Where is your source xml document?

What is the result that you want to get?

What output do you get instead?

What are the essential properties of the transformation?

What does it mean "the HTML doesn't work" ?


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
M

Mark Constant

Dimitre Novatchev said:
Unless somebody is a clairevoyant (and also knows xslt) you're probably not
going to get any help.

Where is your source xml document?

What is the result that you want to get?

What output do you get instead?

What are the essential properties of the transformation?

What does it mean "the HTML doesn't work" ?


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Sorry I didn't include everything. Anyways all I am getting is a
string that reads 1ps2slycooperAWittygamethatisfunforeberybodyimages\SlyCooper.jpg
on a html page. When I say "the HTML doesn't work" I mean that the
tables and headings aren't appearing on the page so that the page
doesn't comes out formatted. All I want is a clean list of a picture,
title, and description on a HTML page. I am trying to get a very basic
..xml file working and from there I will build upon it. Below are my
files.

Here is my games.xsd file
<?xml version="1.0" encoding="iso-8859-1" ?>
<xs:schema id="Games" targetNamespace="http://mark/DevelopmentWebsite"
elementFormDefault="qualified" xmlns="http://mark/DevelopmentWebsite"
xmlns:mstns="http://mark/DevelopmentWebsite"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Entertainment">
<xs:complexType>
<xs:sequence>
<xs:element name="GameList">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:positiveInteger" />
<xs:element name="Console" type="xs:string" />
<xs:element name="Title" type="xs:string" />
<xs:element name="Description" type="xs:string" />
<xs:element name="Image" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

This is my games.xml file
<?xml version="1.0" encoding="utf-8" ?>
<Entertainment xmlns="http://mark/DevelopmentWebsite">
<GameList>
<ID>1</ID>
<Console>PS2</Console>
<Title>Sly Cooper</Title>
<Description>A fun game for everybody</Description>
<Image>images\SlyCooper.jpg</Image>
</GameList>
</Entertainment>

This is my games.xslt file
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lc="http://mark/DevelopmentWebsite">
<xsl:template match="lc">
<h2>My Game Collection</h2>
<table border="1">
<xsl:for-each select="lc:Entertainment/GameList">
<tr>
<th colspan="2"><xsl:value-of select="lc:Title"/></th>
</tr>
<tr>
<td>
<xsl:element name="lc:IMG">
<xsl:attribute name="lc:SRC">
<xsl:value-of select="lc:Image"/>
</xsl:attribute>
</xsl:element>
</td>
<td><xsl:value-of select="lc:Description"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
 
D

Dimitre Novatchev

This is my games.xml file
<?xml version="1.0" encoding="utf-8" ?>
<Entertainment xmlns="http://mark/DevelopmentWebsite">
<GameList>
<ID>1</ID>
<Console>PS2</Console>
<Title>Sly Cooper</Title>
<Description>A fun game for everybody</Description>
<Image>images\SlyCooper.jpg</Image>
</GameList>
</Entertainment>

This is my games.xslt file
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lc="http://mark/DevelopmentWebsite">
<xsl:template match="lc">

There is no element named "lc" in your source.xml -- this template is never
instantiated.

This completely explains what you get (this is the result of the XSLT
built-in templates) -- just the concatenation of all text nodes.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
M

Mark Constant

Dimitre Novatchev said:
There is no element named "lc" in your source.xml -- this template is never
instantiated.

This completely explains what you get (this is the result of the XSLT
built-in templates) -- just the concatenation of all text nodes.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

I took out the <xsl:template match="lc"> in my games.xslt file and
changed it back to <xsl:template match="/">. In my games.xml file I
changed
xmlns="http://mark/DevelopmentWebsite" to
xmlns:lc="http://mark/DevelopmentWebsite" to match the prefix in my
..xslt file. This causes my heading to appear from my HTML but once
again I go back to getting nothing from my xml file. I have read many
many tutorials on the internet and I still don't seem to have a clear
idea of what I am missing.
 
D

Dimitre Novatchev

So, what is your latest xslt code? (I assume that the source.xml hasn't
changed).


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


Mark Constant said:
There is no element named "lc" in your source.xml -- this template is never
instantiated.

This completely explains what you get (this is the result of the XSLT
built-in templates) -- just the concatenation of all text nodes.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

I took out the <xsl:template match="lc"> in my games.xslt file and
changed it back to <xsl:template match="/">. In my games.xml file I
changed
xmlns="http://mark/DevelopmentWebsite" to
xmlns:lc="http://mark/DevelopmentWebsite" to match the prefix in my
.xslt file. This causes my heading to appear from my HTML but once
again I go back to getting nothing from my xml file. I have read many
many tutorials on the internet and I still don't seem to have a clear
idea of what I am missing.[/QUOTE]
 
M

Mark Constant

Dimitre Novatchev said:
So, what is your latest xslt code? (I assume that the source.xml hasn't
changed).


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

My xslt code has gone back to
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lc="http://mark/DevelopmentWebsite">
<xsl:template match="/">
<h2>My Game Collection</h2>
<table border="1">
<xsl:for-each select="lc:Entertainment/GameList">
<tr>
<th colspan="2"><xsl:value-of select="lc:Title"/></th>
</tr>
<tr>
<td>
<xsl:element name="lc:IMG">
<xsl:attribute name="lc:SRC">
<xsl:value-of select="lc:Image"/>
</xsl:attribute>
</xsl:element>
</td>
<td><xsl:value-of select="lc:Description"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

The only thing that has changed in my source.xml file is I changed my
default namespace from xmlns="http://mark/DevelopmentWebsite to
xmlns:lc="http://mark/DevelopmentWebsite so it can match the prefix in
my xslt file. The xslt file above is only outputting my heading from
my HTML code and nothing else. By the way thank you for helping me
this far. I have been on many forums and usually I have seen that the
most knowledgable people on the forum give new-comers a tough time.
 
D

Dimitre Novatchev

My xslt code has gone back to
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lc="http://mark/DevelopmentWebsite">
<xsl:template match="/">
<h2>My Game Collection</h2>
<table border="1">
<xsl:for-each select="lc:Entertainment/GameList">

Here's the problem:

lc:Entertainment/GameList

will not select any node, because there isn't any element named "GameList"
belonging to no namespace.

Must be:

lc:Entertainment/lc:GameList

[snip]
The only thing that has changed in my source.xml file is I changed my
default namespace from xmlns="http://mark/DevelopmentWebsite to
xmlns:lc="http://mark/DevelopmentWebsite so it can match the prefix in
my xslt file.

This was not necessary -- with the correction above the transformation will
work on your original xml document.

Why? Because in selecting/matching elements what is important is not the
prefix used but that the expanded name (namespace-uri, local-name() ) is the
same.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
M

Mark Constant

Dimitre Novatchev said:
My xslt code has gone back to
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lc="http://mark/DevelopmentWebsite">
<xsl:template match="/">
<h2>My Game Collection</h2>
<table border="1">
<xsl:for-each select="lc:Entertainment/GameList">

Here's the problem:

lc:Entertainment/GameList

will not select any node, because there isn't any element named "GameList"
belonging to no namespace.

Must be:

lc:Entertainment/lc:GameList

[snip]
The only thing that has changed in my source.xml file is I changed my
default namespace from xmlns="http://mark/DevelopmentWebsite to
xmlns:lc="http://mark/DevelopmentWebsite so it can match the prefix in
my xslt file.

This was not necessary -- with the correction above the transformation will
work on your original xml document.

Why? Because in selecting/matching elements what is important is not the
prefix used but that the expanded name (namespace-uri, local-name() ) is the
same.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


Thank you so much Dimitre! It works perfectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top