tags:

views:

23

answers:

1

I am using XSLTJSON to convert my XML to JSON. My raw XML is not in the format that I want, so I first pass it through an XSL stylesheet to clean it up and then pass the output of that stylesheet into XSLTJSON.

Right now I'm doing this by calling transformers serially. I'd like to streamline it and have only one call to the transformer necessary. Is there a way to write an XSL stylesheet that includes json.xsl, matches on "/", does it's thing and then passes its output to json:generate()?

A: 

This stylesheet:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:json="http://json.org/"&gt;
    <xsl:import href="xml-to-json.xsl"/>
    <xsl:template match="/">
        <xsl:variable name="vFirstPass">
            <xsl:apply-templates/>
        </xsl:variable>
        <xsl:value-of select="json:generate($vFirstPass)"/>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Alejandro
I want to apply my stylesheet first (it has a template that matches on "/"), and then pass the output from that to json:generate() from the imported json.xsl. It looks like just a minor adjustment from what you have, but I'm not quite sure what to change. I've never worked with modes before.
Danny Cohn
@Danny Cohn: Are you ussing XSLT 2.0 or XSLT 1.0 version?
Alejandro
I'm using XSLT 2.0
Danny Cohn
Danny, which version of which XSLT 2.0 processor do you use? Saxon 9 has a feature http://www.saxonica.com/documentation/extensions/output-extras/next-in-chain.xml that allows you to chain two stylesheets.
Martin Honnen