How do I remove or stop empty namespace created when apply xsl code to xml?  
Author Message
Vaish





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

Hi

I have a code to remove all empty elements useing XSL but the out put xml file look like the one shown below

My question is How do I remove or stop empty namespace (xmlns="") beieng created.

Note: My xsl code top element has <TalkMessage xmlns=" http://www.hide-link.com/ ">
If I use <TalkMessage> then the output xml file is fine

Xsl code

<xsl:stylesheet version="1.0" xmlns:xsl=" http://www.hide-link.com/ ">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="DataSet">
<TalkMessage xmlns=" http://www.hide-link.com/ ">
'''''''
''''''''
'''''''''''''

Dataset

<DataSet xmlns=" http://www.hide-link.com/ ">

<Tbl1>

<A>aaa</A>

<B>bbb</B>

</Tbl1>

<Tbl2>

<a1>one</a1>

<a2>APPLE</a2>

<a3>three</a3>

</Tbl2>
<Tbl2>

<a1>one</a1>

<a2>APPLE</a2>

<a3>three</a3>

</Tbl2>

<Tbl2>

<a1>one</a1>

<a2>PEACH</a2>

<a3>three</a3>

</Tbl2>

</DataSet>

Sample out put xml file

<TalkMessage xmlns=" http://www.hide-link.com/ ">

<Body>

<Retail>

<book>aaa</book>

<Pen>bbb</Pen>

</Retail>

<Tropical>

<Grade xmlns=""> - created empty namespace

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>
<Grade>

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>

</Tropical>

<Organic>

<Sample xmlns=""> - created empty namespace

<No>one</No>

<Fruit>PEACH</Fruit>

<Qty>three</Qty>

</Sample>

</Organic>

</Body>

</TalkMessage>




.NET Development13  
 
 
Martin Honnen





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top


My question is How do I remove or stop empty namespace (xmlns="") beieng created.

Note: My xsl code top element has <TalkMessage xmlns="http://www.mydomain.co.uk/CM/envelope">
If I use <TalkMessage> then the output xml file is fine

Xsl code

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="DataSet">
<TalkMessage xmlns="http://www.mydomain.co.uk/CM/envelope">

If you want to have all literal result elements in the namespace http://www.mydomain.co.uk/CM/envelope then use e.g

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.mydomain.co.uk/CM/envelope" version="1.0">



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

In your stylesheet, which you haven't shown to us, you are creating elements in no namespace, which are descendents of "TalkMessage", which is in the "http://www.mydomain.co.uk/CM/envelope" namespace.

This is why, xmlns="" is generated on all such elements -- to make them not in the "http://www.mydomain.co.uk/CM/envelope" namespace, but in no namespace.

If all elements must reside in the "http://www.mydomain.co.uk/CM/envelope" namespace (and in this case they will not have to be designated in no namespace by adding xmlns="") , this is to be specified explicitly in the xslt stylesheet code.

For example, instead of writing:

<Grade>

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>

write

<Grade xmlns="http://www.mydomain.co.uk/CM/envelope">

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>

A shorter way (if you do not need to have any elements in no-namespace, would be to set the "http://www.mydomain.co.uk/CM/envelope" as the default namespace for the whole stylesheet:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.mydomain.co.uk/CM/envelope"
>

Cheers,
Dimitre Novatchev


 
 
Vaish





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

Hi

<xsl:stylesheet version="1.0xmlns:xsl="http://www.w3.org/1999/XSL/Transformxmlns="http://www.mydomain.co.uk/CM/envelope" >

The above solution works. It is inserting xmlns="http://www.mydomain.co.uk/CM/envelope" to top element of output xml and all other sub elements are empty (eg.  <Sample>) -This howt I wanted

But I want one element to have different namespace (eg From <Grade> to <Grade xmlns="http://www.somdomain.co.uk"> ) - as in the out put xml file

In short I want only two elements to have namespace

(eg <TalkMessage xmlns="http://www.mydomain.co.uk/CM/envelope"> &<Grade mlns="http://www.somdomain.co.uk">

       ------------------------------------------------------------SORRY FOR PESTERING-----------------------------------------------------------------------------

<DataSet>

 <Tbl1>

  <A>aaa</A>

  <B>bbb</B>

 </Tbl1>

 <Tbl2>

   <a1>one</a1>

   <a2>APPLE</a2>

   <a3>three</a3>

   </Tbl2>
<Tbl2>

   <a1>one</a1>

   <a2>APPLE</a2>

   <a3>three</a3>

  </Tbl2>

 <Tbl2>

   <a1>one</a1>

   <a2>PEACH</a2>

   <a3>three</a3>

   </Tbl2>

</DataSet>

 

Sample out put xml file

 <TalkMessage xmlns="http://www.mydomain.co.uk/CM/envelope">

<Body>

  <Retail>

    <book>aaa</book>

    <Pen>bbb</Pen>

  </Retail>

  <Tropical>

        <Grade xmlns="http://www.somdomain.co.uk">

           <No>one</No>

           <Fruit>APPLE</Fruit>

           <Qty>three</Qty>

        </Grade>
       <Grade>

           <No>one</No>

           <Fruit>APPLE</Fruit>

           <Qty>three</Qty>

        </Grade>  

    </Tropical>

  <Organic>

       <Sample>

         <No>one</No>

         <Fruit>PEACH</Fruit>

         <Qty>three</Qty>

       </Sample>

   </Organic>

</Body>

</TalkMessage>



 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

> The above solution works. It is inserting xmlns="http://www.mydomain.co.uk/CM/envelope" to top element of output xml and all other sub elements are empty
> (eg. <Sample>) -This howt I wanted

> But I want one element to have different namespace (eg From <Grade> to <Grade xmlns="http://www.somdomain.co.uk"> ) - as in the out put xml file

> In short I want only two elements to have namespace

> (eg <TalkMessage xmlns="http://www.mydomain.co.uk/CM/envelope"> &<Grade mlns="http://www.somdomain.co.uk">

The element, which you want to put in a special namespace, should be created using the xsl:element instruction, specifying the specific namespace in the "namespace" attribute, something like this:

<xsl:stylesheet version="1.0"

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

<xsl:output omit-xml-declaration="yes"/>

<xsl:template match="/">

<xsl:element name="grade"

namespace="http://www.somdomain.co.uk">

<!-- Whatever else to be done here -->

</xsl:element>

</xsl:template>

</xsl:stylesheet>

When applied on any xml document (not used) the above transformation produces:

<grade xmlns="http://www.somdomain.co.uk" />

Cheers,
Dimitre Novatchev


 
 
Vaish





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

Hi Dim

I should have told you before that I have some children nodes. I apologize!

When I apply for the <xsl:element.... > two other child node got the namespace of the top element <Talkmessage>.

If I use <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> and <xsl:element..> for top element (<TalkMessage> and for <Grade> element (<Grade....namespace....> , I am getting empty namspaces like below

<Box xmlns=""> ,<Square xmlns=""> etc

Note: I am using another xml to remove all the empty elements. Will this xsl be useful to remove the unwanted namspace

Any Idea

Part of xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.mydomain.co.uk/CM/envelope" version="1.0" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="SDLTData">
<TalkMessage>
----

----

---

<xsl:element name="Grade" namespace="http://www.somdomain.co.uk">

child node

Child node

siblings

Child Node

</xsl:element>

<TalkMessage>

Sample out put xml file

<TalkMessage xmlns="http://www.mydomain.co.uk/CM/envelope">

<Body>

<Retail>

<book>aaa</book>

<Pen>bbb</Pen>

</Retail>

<Tropical>

<Grade xmlns="http://www.somdomain.co.uk">

<Box> - Topelement's namspace created here when I use the <xsl:element ..namespace>

<No>one</No>

<Square> - Topelement's namspace created here when I use the <xsl:element ..namespace>

<No>one</No>
<Fruit>APPLE</Fruit>

<wall>

<pack>damaged</pack>

</wall>

<Qty>three</Qty>

</Square>

<Qty>three</Qty>

</Box>

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>
<Grade>

<No>one</No>

<Fruit>APPLE</Fruit>

<Qty>three</Qty>

</Grade>

</Tropical>

<Organic>

<Sample>

<No>one</No>

<Fruit>PEACH</Fruit>

<Qty>three</Qty>

</Sample>

</Organic>

</Body>

</TalkMessage>





 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

> When I apply for the <xsl:element.... > two other child node got the namespace of the top element <Talkmessage>.

> If I use <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> and <xsl:element..> for top
> element (<TalkMessage> and for <Grade> element
(<
Grade....namespace....> , I am getting empty namspaces like below

> <Box xmlns=""> ,<Square xmlns=""> etc

If you know in which namespace you want the "Box" and "Square" elements to be, then you are in full control to produce them in the wanted namespace.

Simply use the xsl:element instruction when creating them and specify the wanted namespace-uri as the value of the "namespace" attribute.

Cheers,
Dimitre Novatchev


 
 
Vaish





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

Hi Dim

 

I want only two elements to have the name space(<Talkmessage> and <IRenvelope>). As you said before I can use <xsl:element > for that. But when I do that it creating empty namspace in the child nodes.

So how do I get rid of that empty namespace I want keep the xml structure as it is, because it has to be sent to other webserver for validation purpose

I want to get the output like below

From <IRheader xmlns=""> to  <IRheader>

The below is the xsl code to remove all the empty elements. Can you look into it to add any code to remove
xmlns="".

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:copy>

    </xsl:copy>
  </xsl:template>
  <xsl:template match="*[not(node())]"/>
</xsl:stylesheet>

The below is the xml structure I am working on

<TalkMessage xmlns="http://www.sample.co.uk/CM/envelope"> 
  <EnvelopeVersion>2.0</EnvelopeVersion>
 +<Header>
 +<TalkDetails>
  <Body>
     <IRenvelope xmlns="http://www.validatio.co.uk">
       <IRheader xmlns="">  -----------------------------------------------I need this to be <IRheader>
         <TestMessage>0</TestMessage>                                    
         <Keys>                                                                               These two elements getting empty    
           <Key Type="STORN">STORN</Key>                         namespace when I apply <xsl:elements>
        </Keys>                                                                               to  <TalkMessage> and <IRenvelope>.
         <PeriodEnd>2002-01-01</PeriodEnd>                         So I want to get rid of xmlns="" from 
         <DefaultCurrency>GBP</DefaultCurrency>                    those two elements
   </IRheader>
   <SDLT xmlns="">      -------------------------------------------------need this to be <SDLT>
      <UTRN>791555</UTRN>
      <TransactionDetails>
         <TransactionDescription>F</TransactionDescription>
         <EffectiveDate>2006-04-05</EffectiveDate>
         <RestrictionsAffecting Apply="Yes">
           <Details>test nine</Details>
         </RestrictionsAffecting>
         <ContractDate>2006-03-24</ContractDate>
         <LandExchanged Exchanged="Yes">
           +<Address>
         </LandExchanged>
        <PursuantToOption>No</PursuantToOption>
      + <TaxCalculation>
      </TransactionDetails>
   +<LandDetail>
   +<VendorDetails>
   +<PurchaserDetails>
   +<Supplementary>
   +<SDLT4>
   +<SDLT4>
  </SDLT>
 </IRenvelope>
 </Body>
</TalkMessage>

-


 
 
Martin Honnen





PostPosted: XML and the .NET Framework, How do I remove or stop empty namespace created when apply xsl code to xml? Top

That stylesheet that you have shown in your last post simply copies elements through to the result tree, it does not change namespaces of elements. Thus if you get e.g. <SDLT xmlns=""> in the result then that is only a copy of what is in the input

So either you already have those xmlns="" in the input or your stylesheet has more templates than you have shown. Generally you have to understand that namespace declarations or undeclarations like xmns="someURI" or xmlns="" are in scope for the element they appear on at any descendants, unless there is a new definition on the descendants. That means if you have e.g.

<element1 xmlns="">

<element2/>

<element3/>

</element1>

then all three elements, element1, element2, element3 are in no namespace.

The same way, if you have e.g

<element1 xmlns="http://example.com/2007/ns1">

<element2/>

<element3/>

</element1>

then all three elements are in the namespace http://example.com/2007/ns1.

If a stylesheet has a literal result element in a certain namespace e.g.

<element1 xmlns="http://example.com/2007/ns1">

but then inside of that elements copies elements from the input tree which are in no namespace then when the result is serialized you get e.g.

<element2 xmlns="">

as the full element includings its namespace is copied and the serializer has to add the undeclaration to preserve the element that has been copied from the input.

It is not quite clear how your input and your complete stylesheet looks but what you complain about seems to have the reasons described above. To fix that your stylesheet cannot simply copy elements, it has to correct the namespace too where needed e.g.

<IRenvelope xmlns="http://www.validatio.co.uk/">

<xsl:apply-templates mode="vnamespace"/>

</IRenvelope>

<xsl:template match="* mode="vnamespace">

<xsl:element name="{local-name()}" namespace="http://www.validatio.co.uk/">

<xsl:apply-templates mode="vnamespace"/>

</xsl:template>