dimanche 2 août 2015

Include multiple similar XSLT files without conflict or overriding templates

I'm trying to devise a way to store data in XSLT files, yet also process them using XSLT to produce an XML file at the end. These are my two source XSLT files, and the result I am after:

File 1:

<fruit>
    <xsl:variable name="amount">10</xsl:variable>
    <type>apple</type>
    <quantity><xsl:value-of select="$amount"/></quantity>
    <remaining><xsl:value-of select="$amount"/></remaining>
</fruit>

File 2:

<fruit>
    <xsl:variable name="amount">20</xsl:variable>
    <type>banana</type>
    <quantity><xsl:value-of select="$amount"/></quantity>
    <remaining><xsl:value-of select="$amount"/></remaining>
</fruit>

Output:

Fruit: apple 10/10
Fruit: banana 20/20

I have no problem writing the XSLT code to take a single XML file and produce the output I want, but I am stuck with how to combine the two files together. If I use <xsl:include>, then I need to wrap the files in <xsl:template> elements which is fine, but then I will have multiple templates the same so I'll either get an error, or with <xsl:import> one will be overridden by the other.

I eventually want to extend this to more than two files without having to alter my XSLT code beyond the include/import lines, so it won't work giving each template a custom name, because I want my global XSLT to be able to apply a transform to all <fruit> elements, without having to specify each template name manually.

Is there any way to achieve this?

Aucun commentaire:

Enregistrer un commentaire