[u]General description:</u>
I have two xslt-transformations that are almost indentical except for the namespaces. I want to share as much as possible by using the include statement but I ran into problem with the namespaces.
[u]Details:</u>
I have two messages to only differ in namespace.
Message 1: -- message1.xml --
Code:
<?xml version="1.0" encoding="utf-8" ?>
<message sender="me" receiver="you" xmlns="http://mycompany/messages/2.0">
<header title="some title"/>
<body parts="2"/>
</message>
Message 2: -- message2.xml --
Code:
<?xml version="1.0" encoding="utf-8" ?>
<message sender="me" receiver="you" xmlns="http://mycompany/messaging/3.0">
<header title="some title"/>
<body parts="2"/>
</message>
I have a simple xslt-transformation for these messages that also only differs in namespace.
Transformation for Message1 is: -- simpletransformation.xslt --
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://mycompany/messages/2.0" xmlns:ms="http://mycompany/messages/2.0">
<xsl:import href="attributes.xslt"/>
<xsl:output method="xml" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/ms:message">
<xsl:variable name="parts"><xsl:call-template name="GetBodyParts" /></xsl:variable>
<xsl:element name="note">
<xsl:choose>
<xsl:when test="$parts='1'">Short message</xsl:when>
<xsl:otherwise>Long message</xsl:otherwise>
</xsl:choose>
from <xsl:value-of select="@sender"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Transformation for Message2 is: -- simpletransformation2.xslt --
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://mycompany/messaging/3.0"
xmlns:ms="http://mycompany/messaging/3.0">
<xsl:import href="attributes.xslt"/>
<xsl:output method="xml" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/ms:message">
<xsl:variable name="parts"><xsl:call-template name="GetBodyParts" /></xsl:variable>
<xsl:element name="note">
<xsl:choose>
<xsl:when test="$parts='1'">Short message</xsl:when>
<xsl:otherwise>Long message</xsl:otherwise>
</xsl:choose>
from <xsl:value-of select="@sender"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Both are sharing the same attributes.xslt: -- attributes.xslt--
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template name="GetBodyParts">
<xsl:value-of select="*/body/@parts" />
</xsl:template>
<xsl:template name="GetTitle">
<xsl:value-of select="*/header/@title" />
</xsl:template>
</xsl:stylesheet>
If you have a close look at simpetransformation.xslt and simpletransformation2.xslt you see that they are the same except for the namespace. So I would like to move the match-parts to the attributes.xslt in order to share as much 'code' as possible. But than I need to add the "ms:" namespace at the top of attributes.xslt making it inpossible to share between the two transformations because otherwise "ms:" is not defined.
-- attributes.xslt--
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://xxxx"
xmlns:ms="http://xxxx">
<xsl:output method="text" />
<xsl:template name="GetBodyParts">
<xsl:value-of select="*/body/@parts" />
</xsl:template>
<xsl:template name="GetTitle">
<xsl:value-of select="*/header/@title" />
</xsl:template>
<xsl:template match="/ms:message">
<xsl:variable name="parts"><xsl:call-template name="GetBodyParts" /></xsl:variable>
<xsl:element name="note">
<xsl:choose>
<xsl:when test="$parts='1'">Short message</xsl:when>
<xsl:otherwise>Long message</xsl:otherwise>
</xsl:choose>
from <xsl:value-of select="@sender"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When I remove the name spaces qualifiers everywhere than it simply does not match the message node from the message since the orignal node has been qualified with a namespace in the xml document.
-- attributes.xslt--
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0>
<xsl:output method="text" />
<xsl:template name="GetBodyParts">
<xsl:value-of select="*/body/@parts" />
</xsl:template>
<xsl:template name="GetTitle">
<xsl:value-of select="*/header/@title" />
</xsl:template>
<xsl:template match="/message">
<xsl:variable name="parts"><xsl:call-template name="GetBodyParts" /></xsl:variable>
<xsl:element name="note">
<xsl:choose>
<xsl:when test="$parts='1'">Short message</xsl:when>
<xsl:otherwise>Long message</xsl:otherwise>
</xsl:choose>
from <xsl:value-of select="@sender"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Is there a way to make this work and to move almost everything in the shared attributes.xslt?
Any suggestion are welcomed.
Dirk