title: A way to split a huge XML file into smaller xml files with XSL
body: <p>I get a huge XML file containing a list of TV broadcasts. And I have to split it up into small files containing all broadcasts for one day only. I managed to to that but have two problems with the xml header and a node being there multiple times.</p>
  
  <p>The structure of the XML is the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;broadcasts&gt;
      &lt;broadcast&gt;
      &lt;id&gt;4637445812&lt;/id&gt;
      &lt;week&gt;39&lt;/week&gt;
      &lt;date&gt;2009-09-22&lt;/date&gt;
      &lt;time&gt;21:45:00:00&lt;/time&gt;
          ... (some more)
      &lt;/broadcast&gt;
      ... (long list of broadcast nodes)
  &lt;/broadcasts&gt;
  </code></pre>
  
  <p>My XSL looks like this:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:redirect="http://xml.apache.org/xalan/redirect"
          extension-element-prefixes="redirect"
          version="1.0"&gt;
      &lt;!-- mark the CDATA escaped tags --&gt;
      &lt;xsl:output method="xml" cdata-section-elements="title text"
          indent="yes" omit-xml-declaration="no" /&gt;
  
      &lt;xsl:template match="broadcasts"&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="broadcast"&gt;
      &lt;!-- Build filename PRG_YYYYMMDD.xml --&gt;
      &lt;xsl:variable name="filename" select="concat(substring(date,1,4),substring(date,6,2))"/&gt;
      &lt;xsl:variable name="filename" select="concat($filename,substring(date,9,2))" /&gt;
      &lt;xsl:variable name="filename" select="concat($filename,'.xml')" /&gt;
      &lt;redirect:write select="concat('PRG_',$filename)" append="true"&gt;    
  
          &lt;schedule&gt;  
          &lt;broadcast program="TEST"&gt;
              &lt;!-- format timestamp in specific way --&gt;
              &lt;xsl:variable name="tmstmp" select="concat(substring(date,9,2),'/')"/&gt;
              &lt;xsl:variable name="tmstmp" select="concat($tmstmp,substring(date,6,2))"/&gt;
              &lt;xsl:variable name="tmstmp" select="concat($tmstmp,'/')"/&gt;
              &lt;xsl:variable name="tmstmp" select="concat($tmstmp,substring(date,1,4))"/&gt;
              &lt;xsl:variable name="tmstmp" select="concat($tmstmp,' ')"/&gt;
              &lt;xsl:variable name="tmstmp" select="concat($tmstmp,substring(time,1,5))"/&gt;
  
              &lt;timestamp&gt;&lt;xsl:value-of select="$tmstmp"/&gt;&lt;/timestamp&gt;
              &lt;xsl:copy-of select="title"/&gt;
              &lt;text&gt;&lt;xsl:value-of select="subtitle"/&gt;&lt;/text&gt;
  
              &lt;xsl:variable name="newVps" select="concat(substring(vps,1,2),substring(vps,4,2))"/&gt;
              &lt;xsl:variable name="newVps" select="concat($newVps,substring(vps,7,2))"/&gt;
              &lt;xsl:variable name="newVps" select="concat($newVps,substring(vps,10,2))"/&gt;
              &lt;vps&gt;&lt;xsl:value-of select="$newVps"/&gt;&lt;/vps&gt;                    
              &lt;nextday&gt;false&lt;/nextday&gt;               
          &lt;/broadcast&gt;      
          &lt;/schedule&gt;
      &lt;/redirect:write&gt;
      &lt;/xsl:template&gt; 
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>My output XMLs are like this:</p>
  
  <p>PRG_20090512.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;schedule&gt;
      &lt;broadcast program="TEST"&gt;
        &lt;timestamp&gt;01/03/2010 06:00&lt;/timestamp&gt;
        &lt;title&gt;&lt;![CDATA[TELEKOLLEG  Geschichte ]]&gt;&lt;/title&gt;
        &lt;text&gt;&lt;![CDATA[Giganten in Fernost]]&gt;&lt;/text&gt;
        &lt;vps&gt;06000000&lt;/vps&gt;
        &lt;nextday&gt;false&lt;/nextday&gt;
      &lt;/broadcast&gt;
    &lt;/schedule&gt;
  &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!-- don't want this --&gt;
    &lt;schedule&gt;  &lt;!-- don't want this --&gt;
      &lt;broadcast program="TEST"&gt;
        &lt;timestamp&gt;01/03/2010 06:30&lt;/timestamp&gt;
        &lt;title&gt;&lt;![CDATA[Die chemische Bindung]]&gt;&lt;/title&gt;
        &lt;text/&gt;
        &lt;vps&gt;06300000&lt;/vps&gt;
        &lt;nextday&gt;false&lt;/nextday&gt;
      &lt;/broadcast&gt;
    &lt;/schedule&gt;
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  ...and so on
  </code></pre>
  
  <p>I can put in omit-xml-declaration="yes" in the output declaration, but the I don't have any xml header. I tried to put in a check if the  tag is already in the output, but failed to select nodes in the output...</p>
  
  <p>This is what I tried:</p>
  
  <pre><code>&lt;xsl:choose&gt;
    &lt;xsl:when test="count(schedule) = 0"&gt; &lt;!-- schedule needed --&gt;   
      &lt;schedule&gt;
        &lt;broadcast&gt;
      ...
    &lt;xsl:otherwise&gt; &lt;!-- no schedule needed --&gt;
      &lt;broadcast&gt;
      ...
  </code></pre>
  
  <p>Thanks for any help, as I'm unaware how to handle that. ;(
  YeTI  </p>