Creating multiple XML files & folders with XSLT

2019-09-17 02:03发布

问题:

I am trying to create an stylesheet to transform an XML to other format of an XML and in the process it should create multiple XML files that are placed in separate folders. The XML file I am trying to work with is very large(~50000 lines) and I want to automate it. So I don't have to hardcode every section. For example, if I have a simple XML like the following:

<Site>
<element run="test1">
    <property name="aaa"/>
    <property name="bbb"/>
    <property name="ccc"/>
    <element run="test2">
        <property name="aaa"/>
        <property name="bbb"/>
        <property name="ccc"/>
        <element run="test3">
            <property name="aaa"/>
            <property name="bbb"/>
            <property name="ccc"/>
        </element>
    </element>
</element>

XSLT should create folder named test1 and have test2, test3 as subfolders (test1/test2/test3) with an XML that consists of the child nodes property in the same folder. So each folder should have small XML in it.

回答1:

Try along the following lines:

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

<xsl:template match="element">
  <xsl:result-document href="{string-join(ancestor-or-self::element/@run, '/')}/properties.xml">
    <root>
      <xsl:copy-of select="property"/>
    </root>
  </xsl:result-document>
</xsl:template>