tags:

views:

43

answers:

1

I want to do an XSLT 1.0 transformation of an xml where i use the node. Now when I apply this it sets an xmlns namespace on the copied items, is it possible to avoid this?

This is the input xml:

 <ns0:Task xmlns:ns0="http://Sharepoint.Task"&gt;
   <UserName>FalcoLannoo</UserName>
   <Title>Task1</Title>
   <Description>Description_0</Description>
   <Library>Library_0</Library>
   <DueDate>1999-05-31</DueDate>
   <Priority>10</Priority>
</ns0:Task>

And I use this xsl to transform it:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"          
xmlns:msxsl="urn:schemas-microsoft-com:xslt"  
xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 ns0" version="1.0" xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/" xmlns:ns1="http://microsoft.com/wsdl/types/" xmlns:s0="http://Sharepoint.Batch" xmlns:ns0="http://Sharepoint.Batch"&gt;
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
     <xsl:apply-templates select="/s0:updates" />
  </xsl:template>
  <xsl:template match="/s0:updates">
    <tns:UpdateListItems>
    <tns:listName>
      <xsl:value-of select="listName/text()" />
    </tns:listName>
    <tns:updates>
      <xsl:copy-of select="/s0:updates/Batch" />
    </tns:updates>
  </tns:UpdateListItems>
 </xsl:template>
</xsl:stylesheet>

And the output file is this:

 <tns:UpdateListItems xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/"    
 xmlns:ns1="http://microsoft.com/wsdl/types/"&gt;
<tns:listName>{58887260-E5EB-4AB5-B105-E5DD57C8C8E0}</tns:listName>
<tns:updates>
    <Batch OnError="Continue" ListVersion="1" ViewName=""  
   xmlns:ns0="http://Sharepoint.Batch"&gt;
        <Method ID="1" Cmd="New">
            <Field Name="UserName">FalcoLannoo</Field>
            <Field Name="Title">Task1</Field>
            <Field Name="Description">Description_0</Field>
            <Field Name="Library">Library_0</Field>
            <Field Name="DueDate">1999-05-31</Field>
            <Field Name="Priority">10</Field>
        </Method>
    </Batch>
</tns:updates>
</tns:UpdateListItems>

And this is the line i want to get rid of: xmlns:ns0="http://Sharepoint.Batch" (in the Batch node)

thx

A: 

First, your input sample doesn't match your output.

Second, from http://www.w3.org/TR/xslt#copy-of

When the result is a node-set, all the nodes in the set are copied in document order into the result tree; copying an element node copies the attribute nodes, namespace nodes and children of the element node as well as the element node itself.

So, this behavior is by design. And it's perfectly logic because in scope namespaces are part of the structure.

In order to strip in scope namespaces you should use this kind of stylesheet:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/"
xmlns:s0="http://Sharepoint.Batch"
exclude-result-prefixes="s0">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="s0:updates">
        <tns:UpdateListItems>
            <tns:listName>
                <xsl:value-of select="listName" />
            </tns:listName>
            <tns:updates>
                <xsl:apply-templates select="Batch" />
            </tns:updates>
        </tns:UpdateListItems>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{name()}">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates select="node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

With this input:

<ns0:updates xmlns:ns0="http://Sharepoint.Batch"&gt;
    <listName>list</listName>
    <Batch OnError="Continue" ListVersion="1" ViewName="">
        <Method ID="1" Cmd="New">
            <Field Name="UserName">FalcoLannoo</Field>
            <Field Name="Title">Task1</Field>
            <Field Name="Description">Description_0</Field>
            <Field Name="Library">Library_0</Field>
            <Field Name="DueDate">1999-05-31</Field>
            <Field Name="Priority">10</Field>
        </Method>
    </Batch>
</ns0:updates>

Output:

<tns:UpdateListItems 
xmlns:tns="http://schemas.microsoft.com/sharepoint/soap/"&gt;
    <tns:listName>list</tns:listName>
    <tns:updates>
        <Batch OnError="Continue" ListVersion="1" ViewName="">
            <Method ID="1" Cmd="New">
                <Field Name="UserName">FalcoLannoo</Field>
                <Field Name="Title">Task1</Field>
                <Field Name="Description">Description_0</Field>
                <Field Name="Library">Library_0</Field>
                <Field Name="DueDate">1999-05-31</Field>
                <Field Name="Priority">10</Field>
            </Method>
        </Batch>
    </tns:updates>
</tns:UpdateListItems>

Edit: More compact code.

Alejandro
**Note to self:** This will work with XML 1.0.- With XML 1.1 the namespace prefix binding should be reset as `<Batch OnError="Continue" ListVersion="1" ViewName="" xmlns:tns="">`
Alejandro