469,658 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

how to include schema declaration in xslt output

I apologize if this has been answered before, I couldn't find it.

I'm trying to transform XML to XML and specify a schema in the output
XML. I am transforming nodes to different local names while keeping
the same namespace. This works fine, until I try to introduce the
schema.
My input looks like this:

<a xmlns="some_url">
<b>doo dah</b>
</a>

I want this output:

<x:c xmlns:x="some_url" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="some_url myschema.xsd">
<x:d>doo dah</x:d>
</x:c>

Here is a stylesheet that works, but doesn't output the schema
location:

<xsl:stylesheet xmlns:x="some_url" xmlns:placeholder="Placeholder"
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:namespace-alias stylesheet-prefix="placeholder" result-
prefix="x"/>
<xsl:template match="x:a">
<placeholder:c>
<xsl:apply-templates select="@* | node()"/>
</placeholder:c>
</xsl:template>
<xsl:template match="x:b">
<placeholder:d>
<xsl:apply-templates select="@* | node()"/>
</placeholder:d>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

I'm using XmlSpy 5.0 with microsoft MSXML selected to do my tests.
If I add the schema declaration to the xsl file it refuses to
transform because the root node <xsl:stylesheetisn't in the schema.
I can't figure out how to tell it to output the schema declaration,
but not to validate the input or the stylesheet itself using that
schema.

Any help would be greatly appreciated!

Aug 6 '07 #1
1 7861
On Aug 6, 2:21 pm, Himself <hims...@davidbrasington.comwrote:
I apologize if this has been answered before, I couldn't find it.

I'm trying to transform XML to XML and specify a schema in the output
XML. I am transforming nodes to different local names while keeping
the same namespace. This works fine, until I try to introduce the
schema.
My input looks like this:

<a xmlns="some_url">
<b>doo dah</b>
</a>

I want this output:

<x:c xmlns:x="some_url" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="some_url myschema.xsd">
<x:d>doo dah</x:d>
</x:c>

Here is a stylesheet that works, but doesn't output the schema
location:

<xsl:stylesheet xmlns:x="some_url" xmlns:placeholder="Placeholder"
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:namespace-alias stylesheet-prefix="placeholder" result-
prefix="x"/>
<xsl:template match="x:a">
<placeholder:c>
<xsl:apply-templates select="@* | node()"/>
</placeholder:c>
</xsl:template>
<xsl:template match="x:b">
<placeholder:d>
<xsl:apply-templates select="@* | node()"/>
</placeholder:d>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

I'm using XmlSpy 5.0 with microsoft MSXML selected to do my tests.
If I add the schema declaration to the xsl file it refuses to
transform because the root node <xsl:stylesheetisn't in the schema.
I can't figure out how to tell it to output the schema declaration,
but not to validate the input or the stylesheet itself using that
schema.

Any help would be greatly appreciated!
I figured it out:

It works if i declare the schema namespace in the xsl but output the
schema location as an attribute. Here is the modified xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:x="some_url" xmlns:placeholder="Placeholder"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:namespace-alias stylesheet-prefix="placeholder" result-
prefix="x"/>

<xsl:template match="x:a">
<placeholder:c>
<xsl:attribute name="xsi:schemaLocation">some_url myschema.xsd</
xsl:attribute>
<xsl:apply-templates select="@* | node()"/>
</placeholder:c>
</xsl:template>

<xsl:template match="x:b">
<placeholder:d>
<xsl:apply-templates select="@* | node()"/>
</placeholder:d>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
Aug 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by william_hulse | last post: by
reply views Thread by Adam Retter | last post: by
4 posts views Thread by Porthos | last post: by
4 posts views Thread by Philip Rayne | last post: by
3 posts views Thread by askar.bektassov | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.