0

I have the following XML structure

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<TransportHeader>
    <Timestamp>2011-01-16 06:00:33</Timestamp>
    <From>
        <Name>DynamicExport</Name>
        <Version>1.</Version>
    </From>
    <MessageId>d7b5c5b69a83</MessageId>
</TransportHeader>
<ExportConfig>
    <DateTimeFormat>yyyy-MM-dd HH:mm:ss</DateTimeFormat>
    <DecimalSymbol>.</DecimalSymbol>
</ExportConfig>
<DataSet> 
    <Tables>
        <Table>
            <RH>...</RH>
            <Rows>  
                <R>Data1</R>
                <R>Data2</R>
                <R>Data3</R>
                <R>Data4</R>
                <R>Data5</R>
            </Rows>
        </Table>
    </Tables>
</DataSet>
</ExportData>

and I need to split the file depending on the amounts of <R>elements. If there are more than 3 <R> elements a second output file needs to be generated. Both files also need the header info.

I came up with this XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:redirect="http://xml.apache.org/xalan/redirect"
extension-element-prefixes="redirect"
exclude-result-prefixes="xd"
version="1.0">

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text() | @*"/>

<xsl:template match="Rows" name="Rows"> 
    <Rows>    
        <xsl:for-each select="R">
            <xsl:variable name="filename1" select="concat('output1','.xml')"/>
            <xsl:variable name="filename2" select="concat('output2','.xml')"/>
            <xsl:variable name="nodePosition" select="position()" />
            <xsl:if test="$nodePosition &lt; 3">
                <redirect:write select="$filename1">
                    <xsl:copy-of select="." />
                </redirect:write>
            </xsl:if>
            <xsl:if test="$nodePosition = 3 or $nodePosition &gt; 3">
                <redirect:write select="$filename2">
                        <xsl:copy-of select="." />
                </redirect:write> 
            </xsl:if>
        </xsl:for-each>  
    </Rows>        
</xsl:template>
</xsl:stylesheet>

But the two output files that get generated only contain "Data2" and "Data5". Could you help me figuring out why the other 3 data elements are missing? And how can I add the header data?

For the header I came up with this XSLT:

<xsl:template match="//Rows">
    <xsl:apply-templates select="@*|Rows"/>
</xsl:template>

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

which works when I apply it to the XML mentioned. But I could not combine the 2 XSLTs - the output just gets messed up.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Peter
  • 1,786
  • 4
  • 21
  • 40
  • The provided XSLT code is not even wellformed XML. The `redirect:` prefix is not bound to any namespace. Please, correct. You seem to be using some kind of extension element for producing multiple outputs. What extension library are you using? – Dimitre Novatchev May 03 '11 at 13:33
  • Also, the `` instruction lacks namespace definition and at least one other necessary attribute. Please, provide code that can be executed -- for repro purposes and not to confuse your readers. – Dimitre Novatchev May 03 '11 at 13:35
  • Hello Dimitre, thank you for your answer. I corrected my erroneous code. I am using XALAN as the processor but I do not know the extension library. – Peter May 03 '11 at 14:03
  • @Peter: I am not using Xalan. Why don't you consider EXSLT and its `` extension element? If I remember well, Xalan implements EXSLT. http://www.exslt.org/exsl/elements/document/index.html . Or use XSLT 2.0 where the `` instruction is standard? – Dimitre Novatchev May 03 '11 at 14:30
  • @Peter: Check my answer for a complete solution. –  May 03 '11 at 18:45
  • @Dimitre, Thank you for the hint to ``, I will have a look at that in case Alejandro's answer would not work. Best regards, Peter – Peter May 04 '11 at 12:25

2 Answers2

1

This transformation shows how to do the splitting. It is left as an exercise to adapt it to your needs:

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

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

 <xsl:template match="R[position() mod 3 =1]">
  <RGroup>
   <xsl:copy-of select=
    ".|following-sibling::R[not(position() > 2)]"/>
  </RGroup>
 </xsl:template>

 <xsl:template match="R"/>
</xsl:stylesheet>

When applied on this XML document (a fragment of the originally provided one):

<Rows>
    <R>Data1</R>
    <R>Data2</R>
    <R>Data3</R>
    <R>Data4</R>
    <R>Data5</R>
</Rows>

the wanted splitting is produced:

<Rows>
   <RGroup>
      <R>Data1</R>
      <R>Data2</R>
      <R>Data3</R>
   </RGroup>
   <RGroup>
      <R>Data4</R>
      <R>Data5</R>
   </RGroup>
</Rows>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Hello Dimitre, thank you for your help. I will adapt it to my needs but the general logic is working.I will also try to figure out the issue with the header data. Best regards, Peter – Peter May 03 '11 at 14:08
  • This solution also works perfectly without copying the header data. It seems to me not as complicated and thus is it also more robust? – Peter May 17 '11 at 13:28
  • @Peter: Yes, note that this solution is more general than the currently accepted answer. – Dimitre Novatchev May 17 '11 at 13:45
1

This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:redirect="http://xml.apache.org/xalan/redirect"
 extension-element-prefixes="redirect">
    <xsl:strip-space elements="*"/>
    <xsl:param name="pLimit" select="3"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:param name="pGroup"/>
        <xsl:copy>
            <xsl:apply-templates select="node()|@*">
                <xsl:with-param name="pGroup" select="$pGroup"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/">
        <xsl:for-each select="//R[position() = 1 or position() = $pLimit + 1]">
            <redirect:write select="concat('output',position(),'.xml')">
                <xsl:apply-templates select="/node()">
                    <xsl:with-param name="pGroup" select="position()-1"/>
                </xsl:apply-templates>
            </redirect:write>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="R">
        <xsl:param name="pGroup"/>
        <xsl:if test="position() > $pLimit = $pGroup">
            <xsl:call-template name="identity"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

output1.xml:

<ExportData>
    <TransportHeader>
        <Timestamp>2011-01-16 06:00:33</Timestamp>
        <From>
            <Name>DynamicExport</Name>
            <Version>1.</Version>
        </From>
        <MessageId>d7b5c5b69a83</MessageId>
    </TransportHeader>
    <ExportConfig>
        <DateTimeFormat>yyyy-MM-dd HH:mm:ss</DateTimeFormat>
        <DecimalSymbol>.</DecimalSymbol>
    </ExportConfig>
    <DataSet>
        <Tables>
            <Table>
                <RH>...</RH>
                <Rows>
                    <R>Data1</R>
                    <R>Data2</R>
                    <R>Data3</R>
                </Rows>
            </Table>
        </Tables>
    </DataSet>
</ExportData>

output2.xml:

<ExportData>
    <TransportHeader>
        <Timestamp>2011-01-16 06:00:33</Timestamp>
        <From>
            <Name>DynamicExport</Name>
            <Version>1.</Version>
        </From>
        <MessageId>d7b5c5b69a83</MessageId>
    </TransportHeader>
    <ExportConfig>
        <DateTimeFormat>yyyy-MM-dd HH:mm:ss</DateTimeFormat>
        <DecimalSymbol>.</DecimalSymbol>
    </ExportConfig>
    <DataSet>
        <Tables>
            <Table>
                <RH>...</RH>
                <Rows>
                    <R>Data4</R>
                    <R>Data5</R>
                </Rows>
            </Table>
        </Tables>
    </DataSet>
</ExportData>

Note: This is a simple solution for not nested tables. The use of extension-element-prefixes attribute.

  • Hello Alejandro, this solution works perfectly. Thank you! I will have a look at "extension-element-prefixes" - I have never worked with that before. Best regards, Peter – Peter May 04 '11 at 12:34