title: How to split an XML file using XSLT 2.0 in Python
body: <p>I have an XML file which I want to split into multiple XML by processing it through xslt 2.0 file. How do I do it? I am currently using Python 2.7.6.  I have tried to use the following code:</p>
  
  <pre><code>import lxml.etree as ET
  
  dom = ET.parse(xml_filename)
  xslt = ET.parse(xsl_filename)
  transform = ET.XSLT(xslt)
  newdom = transform(dom)
  print(ET.tostring(newdom, pretty_print=True))
  </code></pre>
  
  <p>But this returns None.</p>
  
  <p><strong>Example XML File</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;dataset&gt;
      &lt;name&gt;dataset containing bounding box labels on images&lt;/name&gt;
      &lt;comment&gt;created by BBTag&lt;/comment&gt;
      &lt;tags&gt;
          &lt;tag name="Perimeter-SVT" color="#f9e99c"/&gt;
          &lt;tag name="Perimeter-Vivon" color="#032585"/&gt;
          &lt;tag name="ScoreBoard-Vivon" color="#bf5786"/&gt;
          &lt;tag name="Perimeter-StarSports" color="#12dadd"/&gt;
      &lt;/tags&gt;
      &lt;images&gt;
          &lt;image file="/var/www/html/tamsports.com/resources/videos/STAR_SPORTS_2_20170812/STAR_SPORTS_2_20170812-0011.jpg"&gt;
              &lt;box top="505" left="327" width="56" height="29"&gt;
                  &lt;label&gt;ScoreBoard-Vivon&lt;/label&gt;
              &lt;/box&gt;
              &lt;box top="218" left="387" width="67" height="24"&gt;
                  &lt;label&gt;Perimeter-SVT&lt;/label&gt;
              &lt;/box&gt;
          &lt;/image&gt;
          &lt;image file="/var/www/html/tamsports.com/resources/videos/STAR_SPORTS_2_20170812/STAR_SPORTS_2_20170812-0005.jpg"&gt;
              &lt;box top="254" left="159" width="64" height="23"&gt;
                  &lt;label&gt;Perimeter-Vivon&lt;/label&gt;
              &lt;/box&gt;
              &lt;box top="255" left="225" width="61" height="20"&gt;
                  &lt;label&gt;Perimeter-Vivon&lt;/label&gt;
              &lt;/box&gt;
              &lt;box top="254" left="285" width="63" height="23"&gt;
                  &lt;label&gt;Perimeter-Vivon&lt;/label&gt;
              &lt;/box&gt;
              &lt;box top="253" left="357" width="58" height="24"&gt;
                  &lt;label&gt;Perimeter-Vivon&lt;/label&gt;
              &lt;/box&gt;
              &lt;box top="254" left="424" width="56" height="25"&gt;
                  &lt;label&gt;Perimeter-Vivon&lt;/label&gt;
              &lt;/box&gt;
              &lt;box top="256" left="484" width="65" height="23"&gt;
                  &lt;label&gt;Perimeter-Vivon&lt;/label&gt;
              &lt;/box&gt;
              &lt;box top="507" left="326" width="58" height="26"&gt;
                  &lt;label&gt;ScoreBoard-Vivon&lt;/label&gt;
              &lt;/box&gt;
          &lt;/image&gt;
          &lt;image file="/var/www/html/tamsports.com/resources/videos/STAR_SPORTS_2_20170812/STAR_SPORTS_2_20170812-0009.jpg"&gt;
              &lt;box top="249" left="400" width="59" height="29"&gt;
                  &lt;label&gt;Perimeter-StarSports&lt;/label&gt;
              &lt;/box&gt;
          &lt;/image&gt;
      &lt;/images&gt;
  &lt;/dataset&gt;
  </code></pre>
  
  <p><strong>XSLT 2.0 File</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="2.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;xsl:template match="//dataset/tags"&gt;
        &lt;xsl:for-each select="./tag"&gt;
              &lt;xsl:variable name="tagName" select="@name" /&gt;
  
                  &lt;xsl:result-document method="xml" href="{$tagName}.xml"&gt;
                      &lt;dataset&gt;    
                          &lt;xsl:copy-of select="/dataset/name"/&gt;
                          &lt;xsl:copy-of select="/dataset/comment"/&gt;
                          &lt;tags&gt;
                              &lt;xsl:copy-of select="/dataset/tags/tag[./@name = $tagName]"/&gt;
                          &lt;/tags&gt;
                          &lt;images&gt;
                          &lt;xsl:for-each select="/dataset/images/image[./box/label/text() = $tagName]"&gt;
                              &lt;image&gt; 
                                  &lt;xsl:copy-of select="./@file"/&gt;
                                  &lt;xsl:copy-of select="./box[./label[./text() = $tagName]]"/&gt;
                              &lt;/image&gt;
                          &lt;/xsl:for-each&gt;
                          &lt;/images&gt;
                      &lt;/dataset&gt;
                  &lt;/xsl:result-document&gt;                              
        &lt;/xsl:for-each&gt;
      &lt;/xsl:template&gt;     
  &lt;/xsl:stylesheet&gt;
  </code></pre>