0

I want to create transformation from multiple xml files to just one xml (Output. xml) using Saxon. The xml files will be generated by my test tools, so there is no limitation for how many xml will be available. Suppose in one folder I have several files:

Summaryfile001.xml
Summaryfile002.xml
Detailsfile001.xml
Detailsfile002.xml
SummaryandDetailsAllFiles.html

I want to get only summaryfiles. Inside the SummaryFiles001.xml :

<SummaryFile>
  <testname>file001</testname>    
  <Error>1</Error>
  <test>1</test>
  <skipped>1</skipped>
  <failures>0</failures>
  <dateAndTime>15082015093021</dateAndtime>
</SummaryFile>

I did the transformation to one output match with Junit format report using saxon, so the template:

JUnitFormat.xslt

<xsl:output indent="yes" method="xml"/>

<xsl:param name="fileName" select="'output.xml'" />

<xsl:template match="/">
    <xsl:apply-templates mode="inFile" select="collection('file:///C:/Users/Documents/Resultfiles?select=*.xml;recurse=yes')"/>
</xsl:template>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates  select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="SummaryFile">
  <xsl:variable name="testName" select="//SummaryFile/testname"/>
  <xsl:variable name="test" select="//SummaryFile/test"/>
  <xsl:variable name="Error" select="//SummaryFile/Error"/>
  <xsl:variable name="skipped" select="//SummaryFile/skipped"/>
  <xsl:variable name="dateAndtime" select="//SummaryResultsFile/dateAndtime"/>
  <xsl:variable name="failures" select="//SummaryFile/failures"/>
  <testsuites>
            <testsuite  name="{$testName}"
                        tests="{$test}" 
                        time="{$dateAndTime}"
                        failures="{$failures}"  
                        errors="{$Error}"
                        skipped="{$skipped}">
            </testsuite>
  </testsuites>
</xsl:template>

However, when I'm trying to execute with saxon with this command:

java -jar saxon9he.jar -xslt:JUnitTemplate.xslt -s:C:/Users/Documents/Resultfiles -o:Output.xml

I couldnt do the transformation. It says transformation failed. I can do the transformation only for a single xml, but I couldn't do for multiple xml. So the idea for the xslt:

1. Look for only Summary Files
2. Get all the data from Summary file.xml 
3. Create one output xml contain all summary result file in JUnit format

Anybody know how I can solve the problem?

2 Answers 2

2

Run Saxon with java -jar saxon9he.jar -xslt:JUnitTemplate.xslt -it:main -o:Output.xml and change your code to

<xsl:output indent="yes" method="xml"/>


<xsl:template name="main">
  <testsuites>
    <xsl:apply-templates select="collection('file:///C:/Users/Documents/Resultfiles?select=Summary*.xml;recurse=yes')/SummaryFile"/>
  </testsuites>
</xsl:template>



<xsl:template match="SummaryFile">
            <testsuite  name="{testname}"
                        tests="{testname}" 
                        time="{dateAndtime}"
                        failures="{failures}"  
                        errors="{Error}"
                        skipped="{skipped}">
            </testsuite>

</xsl:template>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Martin Honnen, I though I have already marked you answer last week. It's a working solution. Thanks!
0

You're on the right lines. I would invoke the transformation at a named template, e.g. using -it:main, rather than supplying a dummy source document, but I don't think that's the cause of your problem.

I'm concerned about your statement: "It says transformation failed". Saxon will never say that without outputting information about how and why it failed. Either you're not seeing that information, in which case you need to work out why, or you're not passing it on to us, which would be pretty unhelpful of you.

I suspect the problem is in your invocation parameter -s:C:/Users/Documents/Resultfiles. I think that's the name of a directory rather than an XML file. Saxon does actually have the capability to process an entire directory from the command line (applying the entire stylesheet to each file) but that's not what you are trying to do here. Use the -it:main style of invocation instead.

3 Comments

Hi Michael Kay, I got an error because ( I think) it wants to parse the html. This is the error message I got: Error on line 31 column 3 of SummaryandDetailsAllFiles.html:SXXP0003: Error reported by XML parser: The element type "b" must be terminated by the matching end-tag "</b>". While processing SummaryandDetailsAllFiles.html: org.xml.sax.SAXParseException; systemId:file:/C:/Users/Documents/Resultfiles/SummaryandDetailsAllFilesl; lineNumber: 31; columnNumber: 3; The element type "b" must be terminated by the matching end-tag "</b>". 7 transformation failed
So I assumed that I need to change -xslt to -it:main?
Yes, the message suggests it's trying to process all the files in the -s directory, and they are not all XML. So don't supply a -s option, supply -it:main instead to enter at a named template.

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.