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. The XML i want to transforme looke like so

XML:

<Site> <View name="big_bang"> <Element name="Galaxy"> <Property value="milky_way"/> <Element localizedName="Earth"> <Property localizedName="Adresse" value="1"/> </Element> </Element> </View> <View name="more_big_bang"> <Element name="Galaxy"> <Property value="orion"/> <Element localizedName="otherEarth"> <Property localizedName="otherAdresse" value="10"/> </Element> </Element> </View> </Site>

XSLT:

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

`<xsl:template match="Site">
    <xsl:result-document method="xml" href="{string-join(ancestor-or-self::Site/@name, '/')}/test.xml"> 
        <GraphicsTree>
            <xsl:copy-of select="."/>
        </GraphicsTree>
    </xsl:result-document>
</xsl:template>`

XSLT should create folder for each child node with the name of the value of name or localizedName So each folder should have small XML in it with the copy of Property. But instead of that i get one foder and one XML file in it with whole xml file copied to it. I also use SAXON Processor.

4
  • Try to apply the code stackoverflow.com/questions/26506676/… given as answer to your previous question. If you get stuck, then explain us exactly where. Commented Oct 23, 2014 at 12:09
  • The problem im having is that im getting an error: XTDE1490. There are some errors in this line of code but i'm prety sure that i have made no mistakes. And it does not behave as i want it to :( <xsl:result-document method="xml" href="{string-join(ancestor-or-self::Element/@localizedName, '/')}/{@localizedName}.xml"> Commented Oct 23, 2014 at 12:25
  • 1
    Please edit your question and provide minimal but complete samples of XML and XSLT causing that error, together with some details on the XSLT processor you use. If there are two sibling <Element localizedName="foo"/>, then you would indeed get an error as the code would try to create a file named foo.xml in the same folder twice. You will need to explain how you want to handle that case. Commented Oct 23, 2014 at 12:52
  • There is only a single Site element in your input sample so of course a template with match="Site" that then has one xsl:result-document instruction will create a single output file and if you have an xsl:copy-of select="." then you copy the Site element to that output file. So you need to decide for which elements you want to create an output file, write a template matching them, as shown in my answer to your previous question, and then put the xsl:result-document in that template. Commented Oct 23, 2014 at 14:10

1 Answer 1

2

Based on my previous suggestion, I think for this XML you need an approach like

<xsl:template match="Site">
  <xsl:apply-templates select="//Element[@name | @localizedName]"/>
</xsl:template>

<xsl:template match="Element">
  <xsl:result-document href="{string-join(ancestor-or-self::Element[@name | @localizedName]/(if (@name) then @name else @localizedName), '/')}/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.

Comments

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.