Creating multiple XML files & folders with XSLT

陌路散爱 提交于 2019-12-12 02:06:58

问题


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>


来源:https://stackoverflow.com/questions/26506676/creating-multiple-xml-files-folders-with-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!