RE: [xsl] copying namespaces question

Subject: RE: [xsl] copying namespaces question
From: Robby Pelssers <Robby.Pelssers@xxxxxxx>
Date: Mon, 2 Jul 2012 12:46:04 +0200
Hi Michael,

Interesting approach..  I will definitely check your suggestion out and get
back on this. This would indeed allow me to avoid constructing elements
manually.

Robby

-----Original Message-----
From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
Sent: Monday, July 02, 2012 12:41 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] copying namespaces question

I think I would do it like this:

<xsl:template match="/">
<xsl:for-each select="//mycompany:object">
<xsl:result-document href="{concat($dir, mycompany:name)}">
<xsl:apply-templates select="/*">
<xsl:with-param name="object" select="." tunnel="yes"/>
</
</
</
</

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

     <xsl:template match="mycompany:object">
      <xsl:param name="object" tunnel="yes"/>
       <xsl:if test=". is $object">
         <xsl:next-match/>
       </
     </

Michael Kay
Saxonica


On 02/07/2012 11:16, Robby Pelssers wrote:
> Hi all,
>
> I have a working implementation for the use case below where I need to split
an xml file into chunks.  Although it works I'm not extremely satisfied by
hardcoding the namespace http://www.mycompany.com in my result tree.   Just
checking if this can be improved.  PS. The example is made up and simplified
but showing exactly what's needed.
>
> Kind regards,
> Robby Pelssers
>
>
>
> Input document:
> --------------
> <objects
>    xmlns="www.mycompany.com"
>    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>    creationDateTime="2012-06-21T10:00:10+02:00"
>    transLanguage="EN"
>    baseLanguage="EN"
>    messageID="1340265610203998445">
>    <object>
>      <changedate>2012-06-20T15:42:41+02:00</changedate>
>      <name>PH3330L</name>
>      <parent xsi:nil="true"/>
>    </object>
>    <object>
>      <changedate>2012-06-21T08:53:17+02:00</changedate>
>      <name>BUK9MNP-100</name>
>      <parent xsi:nil="true"/>
>    </object>
> </objects>
>
> Expected output: 2 files called PH3330L.xml and BUK9MNP-100.xml
> -------------------------------------------------------------
> <objects
>    xmlns="www.mycompany.com"
>    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>    creationDateTime="2012-06-21T10:00:10+02:00"
>    transLanguage="EN"
>    baseLanguage="EN"
>    messageID="1340265610203998445">
>    <object>
>      <changedate>2012-06-20T15:42:41+02:00</changedate>
>      <name>PH3330L</name>
>      <parent xsi:nil="true"/>
>    </object>
> </objects>
>
> <objects
>    xmlns="www.mycompany.com"
>    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>    creationDateTime="2012-06-21T10:00:10+02:00"
>    transLanguage="EN"
>    baseLanguage="EN"
>    messageID="1340265610203998445">
>    <object>
>      <changedate>2012-06-21T08:53:17+02:00</changedate>
>      <name>BUK9MNP-100</name>
>      <parent xsi:nil="true"/>
>    </object>
> </objects>
>
> Current implementation:
> ----------------------
> <xsl:stylesheet version="2.0"
>     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>     xmlns:mycompany="www.mycompany.com">
>
>      <xsl:param name="destinationFolder"/>
>
>      <xsl:template match="/">
>        <xsl:apply-templates select="mycompany:objects/mycompany:object"/>
>      </xsl:template>
>
>      <xsl:template name="writeSingleObject">
>        <xsl:param name="thisObject"/>
>        <xsl:param name="fileName"/>
>        <xsl:result-document href="{concat('file:///',$destinationFolder,
'resources/', $fileName, '.xml')}" method="xml">
>          <!--
>             I am currently hardcoding the mycompany namespace as I
reconstruct the parent element manually.
>             Is there some better (dynamic way) to accomplish my use case?
>          -->
>          <xsl:element name="{../local-name()}"
xmlns="http://www.mycompany.com";>
>            <xsl:apply-templates select="../@*"/>
>            <xsl:copy-of select="$thisObject"/>
>          </xsl:element>
>        </xsl:result-document>
>      </xsl:template>
>
>      <xsl:template match="mycompany:object">
>        <xsl:call-template name="writeSingleObject">
>          <xsl:with-param name="thisObject" select="."/>
>          <xsl:with-param name="fileName"   select="mycompany:name"/>
>        </xsl:call-template>
>      </xsl:template>
>
>      <xsl:template match="@* | node()">
>        <xsl:copy copy-namespaces="yes">
>          <xsl:apply-templates select="@*"/>
>          <xsl:apply-templates/>
>        </xsl:copy>
>      </xsl:template>
>
> </xsl:stylesheet>

Current Thread