Re: [xsl] @xmlns retrieving value of

Subject: Re: [xsl] @xmlns retrieving value of
From: Mario Madunic <hajduk@xxxxxxxx>
Date: Wed, 20 Sep 2006 11:52:32 -0700
Thanks George for the reply back but after looking at your code it wasn't
exactly what I required. So I played with it for awhile and here is the solution
I came by

<xsl:variable name="l_CurrentNameSpace" select="namespace::*[name() !=
'xml'][name() != 'props'][last()]" />


this way I can manipulate the namespace itself and get the prefix and uri
without much hassle and test if the var is not empty and output in the result doc.

Thanks for your input without it I would not have been able to find this solution.

Mario Madunic

Quoting George Cristian Bina <george@xxxxxxxxxxxxx>:

> Hi Mario,
> 
> Than just test if the namespace is present and create the element in 
> that namespace, if it is present, and in no namespace otherwise:
> 
> <?xml version="1.0" encoding="utf-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
> version="2.0">
>      <xsl:template match="root">
>          <newDoc>
>              <xsl:apply-templates/>
>          </newDoc>
>      </xsl:template>
>      <xsl:template match="p">
>          <xsl:variable name="pns" select="namespace-uri()"/>
>          <xsl:choose>
>              <xsl:when test="namespace::*[name()!=$pns and 
> name()!='xml'][1]">
>                  <xsl:element name="{name(namespace::*[name()!=$pns and 
> name()!='xml'][1])}:para"
>                      namespace="{namespace::*[name()!=$pns and 
> name()!='xml'][1]}">this is a test to retrieve the
>                      namespace uris and name.</xsl:element>
>              </xsl:when>
>              <xsl:otherwise>
>                  <para>this is a test to retrieve the namespace uris and 
> name.</para>
>              </xsl:otherwise>
>          </xsl:choose>
> 
>      </xsl:template>
> </xsl:stylesheet>
> 
> 
> Best Regards,
> George
> ---------------------------------------------------------------------
> George Cristian Bina
> <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
> http://www.oxygenxml.com
> 
> Mario Madunic wrote:
> > Thanks George for your solution. This isn't exactly what I was looking for
> but
> > that is my fault.
> > 
> > Should have mentioned that I would like to test for the existence of @xmlns
> and
> > output the namespace only then. So I can get output like the following
> > 
> > <anElem:para xmlns:anElem="http://www.namespace.com/namespaces/anElem";>this
> is a
> > test to retrieve the namespace uri and name.</anElem:para>
> > 
> > or if ther is no @xmlns
> > 
> > <para>this is a test to retrieve the namespace uri and name.</para>
> > 
> > from
> > 
> > <root>
> >  ...
> >  <p xmlns:anElem="http://www.namespace.com/namespaces/anElem";>this is a
> test to
> > retrieve the namespace uri and name.</p>
> > 
> >  <p>this is a test to retrieve the namespace uri and name.</p>
> > ...
> > </root>
> > 
> > Sorry about that.
> > 
> > Mario
> > 
> > 
> > Quoting George Cristian Bina <george@xxxxxxxxxxxxx>:
> > 
> >> Hi Mario,
> >>
> >> The document you want as output has the para element with an undeclared 
> >> namespace as the anElem prefix is not bound. Probably what you want is
> >>
> >> <anElem:para 
> >> xmlns:anElem="http://www.namespace.com/namespaces/anElem";>this is a test
> >>          to retrieve the namespace uri and name.</anElem:para>
> >> ?
> >>
> >> In that case you can use something like:
> >> <?xml version="1.0" encoding="utf-8"?>
> >> <x:stylesheet xmlns:x="http://www.w3.org/1999/XSL/Transform";
> >>    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
> >>    <x:template match="root">
> >>      <newDoc>
> >>        <xsl:apply-templates/>
> >>      </newDoc>
> >>    </x:template>
> >>    <xsl:template match="p">
> >>      <xsl:variable name="pns" select="namespace-uri()"/>
> >>      <xsl:for-each select="namespace::*[name()!=$pns and
> name()!='xml'][1]">
> >>        <xsl:element name="{name()}:para" namespace="{.}">this is a test 
> >> to retrieve the namespace uris
> >>          and name.</xsl:element>
> >>      </xsl:for-each>
> >>    </xsl:template>
> >> </x:stylesheet>
> >>
> >> to get
> >>
> >> <?xml version="1.0" encoding="UTF-8"?><newDoc>
> >>      ...
> >>      <anElem:para 
> >> xmlns:anElem="http://www.namespace.com/namespaces/anElem";>this is a test 
> >> to retrieve the namespace uris
> >>          and name.</anElem:para>
> >>      ...
> >> </newDoc>
> >>
> >> Best Regards,
> >> George
> >> ---------------------------------------------------------------------
> >> George Cristian Bina
> >> <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
> >> http://www.oxygenxml.com
> >>
> >>
> >> Mario Madunic wrote:
> >>> Hi I'm hoping somebody might have some insight.
> >>>
> >>> Saxon 8
> >>> XSLT 2
> >>>
> >>> Here is a sample piece of xml to help get the idea of what I'm trying to
> >> achieve.
> >>> <root>
> >>> ...
> >>>      <p xmlns:anElem="http://www.namespace.com/namespaces/anElem";>this is
> a
> >> test
> >>> to retrieve the namespace uri and name.</p>
> >>> ...
> >>> </root>
> >>>
> >>> I've been trying to get the namespace and uri from the @xmlns with no
> >> success. I
> >>> would like to reuse both the ns and uri when I transform the p into
> >> something like
> >>> <newDoc>
> >>> ...
> >>> <anElem:para xmlns="http://www.namespace.com/namespaces/anElem";>this is
> a
> >> test
> >>> to retrieve the namespace uri and name.</anElem:para>
> >>> ...
> >>> </newDoc>
> >>>
> >>> I've used
> >>>
> >>> namespace-uri()
> >>> name()
> >>> namespace-uri-for-prefix()
> >>> namespace-uri-from-QName()
> >>>
> >>> I'm pretty sure the last 2 were being used inappropriately
> >>>
> >>> Any ideas or solution would be appreciated.
> >>>
> >>> Thanks
> >>>
> >>> Marijan Madunic

Current Thread