Search code examples
xmlxsltxslt-2.0transformation

save xml transformation in new xml file


Hej!

I have a xslt file to transform my xml which works perfectly well :) BUT I only get the results in the terminal in oxygen and not in a separate file.

I tried:

<xsl:stylesheet 
xmlns="http://www.tei-c.org/ns/1.0" 
xmlns:telota="http://www.telota.de"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="xs"
version="2.0">

    <xsl:result-document href="new_badius.xml"/>

which gives me the error: "Element xsl:result-document must not appear directly within xsl:stylesheet"

tried some variation of hierarchy:

<xsl:template match="node()|@*"> 
    <xsl:copy> 
        <xsl:result-document href="new_file.xml"/>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

or

<xsl:template match="node()|@*"> 
    <xsl:result-document href="new_file.xml"/>
    <xsl:copy> 
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

which gives me the error only one new_file.xml can be created (which is empty)

Where should I put <xsl:result-document href="new_file.xml"/> or is there a better way to save my outcome as a .xml?


Solution

  • The xsl:result-document belongs into a template, which template obviously depends on both your XML input structure, your stylesheet structure and the number and wanted content of the result document(s?).

    Assuming you want your complete document to be processed to construct one result tree to be written to one result document you might want e.g.

    <xsl:template match="/">
      <xsl:result-document href="new_file.xml">
        <xsl:apply-templates/>
      </xsl:result-document>
    </xsl:template>
    

    That then assumes your stylesheet has some meaningful templates to transform the input elements into the wanted output elements.

    Also note, that oXygen allows you to configure a transformation scenario where you can define that the primary result is written to a file whose name you can define, see https://www.oxygenxml.com/doc/versions/26.1/ug-editor/topics/the-output-tab.html.

    If you run Saxon from the command line, use e.g. -o:new_result.xml.

    If you run Java code or Python code, see the Saxon documentation about the relevant API documentation.