[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] copying namespaces question


Subject: Re: [xsl] copying namespaces question
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Mon, 02 Jul 2012 11:40:56 +0100

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
Keywords
xml