This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Microsoft XML > June 2006 > Append namespace with XSL





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Append namespace with XSL
Paul Yaroshenko

2006-06-27, 8:28 pm

Someone knows how to append namespace using XSL transformation?

For example, I have XML:

<AAA>
<BBB>
<CCC/>
</BBB>
</AAA>

How I can transform it to this from:

<NS:AAA>
<NS:BBB>
<NS:CCC/>
</NS:BBB>
</NS:AAA>


It looks very trivial fro the first sight but actually I'm unable to
complete this task.
----------------------------------------------------------------------
SamePaul
LighthouseRedSKY

2006-06-27, 8:28 pm

You can do this by using XSD.

As a first step you will create a xsd document.

This xsd document will be your namespace.

In your XML document you will add the this xsd document as a namespace with
NS prefix
(i wrote a "prefix" but maybe this description is called with different
definition).

As a result you can define your XML elements with NS.

For more information w3schools.com is very perfect site to learn.


"Paul Yaroshenko" <samepaul@postmark.net> wrote in message
news:O07kTQxlGHA.492@TK2MSFTNGP05.phx.gbl...
> Someone knows how to append namespace using XSL transformation?
>
> For example, I have XML:
>
> <AAA>
> <BBB>
> <CCC/>
> </BBB>
> </AAA>
>
> How I can transform it to this from:
>
> <NS:AAA>
> <NS:BBB>
> <NS:CCC/>
> </NS:BBB>
> </NS:AAA>
>
>
> It looks very trivial fro the first sight but actually I'm unable to
> complete this task.
> ----------------------------------------------------------------------
> SamePaul



Joe Fawcett

2006-06-27, 8:28 pm

You could use a modified version of the identity template:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://myDomain.com/namespaces/1">
<xsl:template match="node()|@*">
<xsl:choose>
<xsl:when test="local-name() = ''">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:element name="ns:{local-name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

--
Joe Fawcett - XML MVP

http://joe.fawcett.name
"Paul Yaroshenko" <samepaul@postmark.net> wrote in message
news:O07kTQxlGHA.492@TK2MSFTNGP05.phx.gbl...
> Someone knows how to append namespace using XSL transformation?
>
> For example, I have XML:
>
> <AAA>
> <BBB>
> <CCC/>
> </BBB>
> </AAA>
>
> How I can transform it to this from:
>
> <NS:AAA>
> <NS:BBB>
> <NS:CCC/>
> </NS:BBB>
> </NS:AAA>
>
>
> It looks very trivial fro the first sight but actually I'm unable to
> complete this task.
> ----------------------------------------------------------------------
> SamePaul



Paul Yaroshenko

2006-06-27, 8:28 pm

Just excellent... Thank you!

Joe Fawcett wrote:
> You could use a modified version of the identity template:
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:ns="http://myDomain.com/namespaces/1">
> <xsl:template match="node()|@*">
> <xsl:choose>
> <xsl:when test="local-name() = ''">
> <xsl:copy>
> <xsl:apply-templates select="node()|@*"/>
> </xsl:copy>
> </xsl:when>
> <xsl:otherwise>
> <xsl:element name="ns:{local-name()}">
> <xsl:apply-templates select="node()|@*"/>
> </xsl:element>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:template>
> </xsl:stylesheet>
>
>

Joe Fawcett

2006-06-27, 8:28 pm

"Paul Yaroshenko" <samepaul@postmark.net> wrote in message
news:eFYTjx4lGHA.3352@TK2MSFTNGP02.phx.gbl...
> Just excellent... Thank you!
>

Just thinking more about your problem...
The example you showed had no attributes, if it did do you want them in a
namespace or not?
If so then use the example I posted, if not use this version:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://myDomain.com/namespaces/1">
<xsl:template match="node()">
<xsl:choose>
<xsl:when test="local-name() = ''">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:element name="ns:{local-name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>


--

Joe Fawcett - XML MVP


http://joe.fawcett.name


Paul Yaroshenko

2006-06-27, 8:28 pm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Yes, I encountered problem with attributes, but this is kind of problem
I'm able to solve by myself :)<br>
I didn't know about 'local-name()' and that you can call function
inside of strings just by enclosing it by {}<br>
<br>
Joe Fawcett wrote:
<blockquote cite="mid%23jHlx56lGHA.2296@TK2MSFTNGP04.phx.gbl"
type="cite">
<pre wrap="">"Paul Yaroshenko" <a class="moz-txt-link-rfc2396E" href="mailto:samepaul@postmark.net">&lt;samepaul@postmark.net&gt;</a> wrote in message
<a class="moz-txt-link-freetext" href="news:eFYTjx4lGHA.3352@TK2MSFTNGP02.phx.gbl">news:eFYTjx4lGHA.3352@TK2MSFTNGP02.phx.gbl</a>...
</pre>
<blockquote type="cite">
<pre wrap="">Just excellent... Thank you!

</pre>
</blockquote>
<pre wrap=""><!---->Just thinking more about your problem...
The example you showed had no attributes, if it did do you want them in a
namespace or not?
If so then use the example I posted, if not use this version:

&lt;xsl:stylesheet version="1.0"
xmlns:xsl=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/1999/XSL/Transform">"http://www.w3.org/1999/XSL/Transform"</a>
xmlns:ns=<a class="moz-txt-link-rfc2396E" href="http://myDomain.com/namespaces/1">"http://myDomain.com/namespaces/1"</a>&gt;
&lt;xsl:template match="node()"&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test="local-name() = ''"&gt;
&lt;xsl:copy&gt;
&lt;xsl:apply-templates select="node()|@*"/&gt;
&lt;/xsl:copy&gt;
&lt;/xsl:when&gt;
&lt;xsl:otherwise&gt;
&lt;xsl:element name="ns:{local-name()}"&gt;
&lt;xsl:apply-templates select="node()|@*"/&gt;
&lt;/xsl:element&gt;
&lt;/xsl:otherwise&gt;
&lt;/xsl:choose&gt;
&lt;/xsl:template&gt;
&lt;xsl:template match="@*"&gt;
&lt;xsl:copy/&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;


</pre>
</blockquote>
<br>
</body>
</html>
Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews