Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT Generating Folders

I am trying to parse an XML file into smaller XML files and place them into folders. The folders get generated based on some of the elements in XML.

How do I generating folders in XSLT? I was able to generate files using xsl:result-document, but so far I had no luck with finding how to generate folders.

like image 958
user1325843 Avatar asked Apr 11 '12 06:04

user1325843


2 Answers

You don't need to create folders specifically, just supply full desired path like folder1/doc1.xml. For example:


<?xml version="1.0" encoding="UTF-8"?>
<tests>
    <testrun run="test1">
        <test name="foo" pass="true" />
        <test name="bar" pass="true" />
        <test name="baz" pass="true" />
    </testrun>
    <testrun run="test2">
        <test name="foo" pass="true" />
        <test name="bar" pass="false" />
        <test name="baz" pass="false" />
    </testrun>
    <testrun run="test3">
        <test name="foo" pass="false" />
        <test name="bar" pass="true" />
        <test name="baz" pass="false" />
    </testrun>
</tests>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="2.0">

<xsl:output method="text"/>
<xsl:output method="html" indent="yes" name="html"/>

<xsl:template match="/">
<xsl:for-each select="//testrun">
<xsl:variable name="filename"
  select="concat('output1/',@run,'.html')" />
<xsl:value-of select="$filename" />  <!-- Creating  -->
<xsl:result-document href="{$filename}" format="html">
    <html><body>
        <xsl:value-of select="@run"/>
    </body></html>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
like image 84
Petr Abdulin Avatar answered Oct 06 '22 00:10

Petr Abdulin


It's probably implementation-dependent, but if you use Saxon then <xsl:result-document href="foo/bar/baz/sample.xml"/> will create the folders if they don't already exist.

like image 23
Michael Kay Avatar answered Oct 05 '22 23:10

Michael Kay