Reputation: 974
When working on XML transformation, I would like to combine multiple XSLs so I can reuse code snippets in an easy way.
The following works: Gender.xslt
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/person/gender" name="gender">
<Geslacht>
<Code>
<xsl:apply-templates select="genderCode" />
</Code>
<Omschrijving>
<xsl:if test="genderCode=1">Mannelijk</xsl:if>
<xsl:if test="genderCode=2">Vrouwlijk</xsl:if>
</Omschrijving>
</Geslacht>
</xsl:template>
</xsl:stylesheet>
When applying to the following XML file:
<?xml version="1.0" encoding="UTF-16" ?>
<person>
<gender>
<genderCode>1</genderCode>
</gender>
</person>
I get the following expected outcome
<?xml version="1.0" encoding="UTF-8"?>
<Geslacht>
<Code>1</Code>
<Omschrijving>Mannelijk</Omschrijving>
</Geslacht>
In a similar way, I transform an identifier:
ID.xslt
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/person" name="ssin">
<INSZ>
<xsl:apply-templates select="ssin" />
</INSZ>
</xsl:template>
</xsl:stylesheet>
Applied to
<?xml version="1.0" encoding="UTF-16" ?>
<person>
<ssin>123456789</ssin>
</person>
Gives
<?xml version="1.0" encoding="UTF-8"?>
<INSZ>123456789</INSZ>
Now I would like to combine the two
Given the following input:
<?xml version="1.0" encoding="UTF-16" ?>
<person>
<ssin>1234567891234</ssin>
<gender>
<genderCode>1</genderCode>
</gender>
</person>
I would like to obtain the following result
<?xml version="1.0" encoding="UTF-8"?>
<Persoon>
<INSZ>1234567891234</INSZ>
<Geslacht>
<Code>1</Code>
<Omschrijving>Mannelijk</Omschrijving>
</Geslacht>
</Persoon>
My Stylesheet:
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="Gender.xslt" />
<xsl:include href="ID.xslt" />
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Persoon>
<xsl:call-template name="gender" />
<xsl:call-template name="ssin" />
<xsl:apply-imports />
</Persoon>
</xsl:template>
The problem is that the gender stylesheet gets called, but the result is empty; structure but no data. The SSIN part works like a charm.
Any help is much appreciated!
Barry
Upvotes: 0
Views: 68
Reputation: 167401
I would suggest to implement the first transformation as
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="gender-descriptions" as="xs:string*"
select="'Mannelijk', 'Vrouwlijk'"/>
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="person/gender">
<Geslacht>
<xsl:apply-templates/>
</Geslacht>
</xsl:template>
<xsl:template match="gender/genderCode">
<Code>
<xsl:value-of select="."/>
</Code>
<Omschrijving>
<xsl:value-of select="$gender-descriptions[position() = current()]"/>
</Omschrijving>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/gWvjQfG
The second transformation could be implemented as as already indicated in the comment:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="person/ssin">
<INSZ>
<xsl:apply-templates/>
</INSZ>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/gWvjQfG/1
Then the importing stylesheet would simply do
<xsl:template match="person">
<Persoon>
<xsl:apply-templates/>
</Persoon>
</xsl:template>
(https://xsltfiddle.liberty-development.net/gWvjQfG/2 has all templates included textually, not imported, but the result is as you want it).
Upvotes: 1