0

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 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick answer! Could you also point me to some sort of tutorial where i could learn what this could meen? "{string-join(ancestor-or-self::element/@run, '/')} I assume that this would be some kind of the XPath expresion's??
The {expression} syntax is the syntax XSLT defines as attribute value templates (w3.org/TR/xslt20/#dt-attribute-value-template) where you can put an XPath expression in curly braces to compute (parts of) an attribute value. The expression string-join(ancestor-or-self::element/@run, '/') is indeed an XPath (2.0) expression, merely a function call to the string-join function. See maxtoroq.github.io/xpath-ref for reference or use an XPath 2.0 tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.