Multiple XSLT files to single XSLT file for 2 different xml files
This is my xml file:
<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
<CONTACT>
<FirstName>Ford</FirstName>
<LastName>Pasteur</LastName>
<EMail>pasteur.ford@yahoo.com</EMail>
</CONTACT>
<CONTACT>
<FirstName>Jack</FirstName>
<LastName>Sully</LastName>
<URL>http://www.facebook.com/profile.php?id=1000474277</URL>
</CONTACT>
<CONTACT>
<FirstName>Colombo</FirstName>
<LastName>Chao</LastName>
<EMail>chao.colombo@liberto.it</EMail>
</CONTACT>
</CONTACTS>
I used below XSLT file for my fist version of xml output.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CONTACT">
<xsl:copy>
<Customer-ID>
<xsl:value-of select="generate-id(.)"/>
</Customer-ID>
<xsl:copy-of select="FirstName|LastName|URL"/>
<Facebook-ID>
<xsl:choose>
<xsl:when test="URL">
<xsl:value-of select="substring-after(URL,'?id=')"/>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</Facebook-ID>
<EMAILS>
<xsl:apply-templates select="EMail"/>
</EMAILS>
</xsl:copy>
</xsl:template>
<xsl:template match="EMail">
<EMail>
<Type><xsl:value-of select="substring-before(
substring-after(.,'@'),
'.')"/>
</Type>
<Value><xsl:value-of select="."/></Value>
</EMail>
</xsl:template>
</xsl:stylesheet>
My first version of xml output from the above XSLT file:
<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
<CONTACT>
<Customer-ID>N65539</Customer-ID>
<FirstName>Ford</FirstName>
<LastName>Pasteur</LastName>
<EMAILS>
<EMail>
<Type>yahoo</Type>
<Value>pasteur.ford@yahoo.com</Value>
</EMail>
</EMAILS>
</CONTACT>
<CONTACT>
<Customer-ID>N65546</Customer-ID>
<FirstName>Jack</FirstName>
<LastName>Sully</LastName>
<URL>http://www.facebook.com/profile.php?id=1000474277</URL>
<Facebook-ID>1000474277</Facebook-ID>
<EMAILS/>
</CONTACT>
<CONTACT>
<Customer-ID>N65553</Customer-ID>
<FirstName>Colombo</FirstName>
<LastName>Chao</LastName>
<EMAILS>
<EMail>
<Type>liberto</Type>
<Value>chao.colombo@liberto.it</Value>
</EMail>
</EMAILS>
</CONTACT>
</CONTACTS>
This is my second XSLT file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CONTACT">
<xsl:copy>
<Customer-ID>
<xsl:value-of select="Customer-ID"/>
</Customer-ID>
<FirstName>
<xsl:value-of select="FirstName"/>
</FirstName>
<LastName>
<xsl:value-of select="LastName"/>
</LastName>
<gmail>
<xsl:value-of select="EMAILS/EMail[Type='gmail']/Value"/>
</gmail>
<yahoo>
<xsl:value-of select="EMAILS/EMail[Type='yahoo']/Value"/>
</yahoo>
<liberto>
<xsl:value-of select="EMAILS/EMail[Type='liberto']/Value"/>
</liberto>
<URL>
<xsl:value-of select="URL"/>
</URL>
<Facebook-ID>
<xsl:value-of select="Facebook-ID"/>
</Facebook-ID>
</xsl:copy>
</xsl:template>
This is my final xml output from the 2nd XSLT file:
<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
<CONTACT>
<Customer-ID>N65539</Customer-ID>
<FirstName>Ford</FirstName>
<LastName>Pasteur</LastName>
<gmail/>
<yahoo>pasteur.ford@yahoo.com</yahoo>
<liberto/>
<URL/>
<Facebook-ID/>
</CONTACT>
<CONTACT>
<Customer-ID>N65546</Customer-ID>
<FirstName>Jack</FirstName>
<LastName>Sully</LastName>
<gmail/>
<yahoo/>
<liberto/>
<URL>http://www.facebook.com/profile.php?id=1000474277</URL>
<Facebook-ID>1000474277&l开发者_高级运维t;/Facebook-ID>
</CONTACT>
<CONTACT>
<Customer-ID>N65553</Customer-ID>
<FirstName>Colombo</FirstName>
<LastName>Chao</LastName>
<gmail/>
<yahoo/>
<liberto>chao.colombo@liberto.it</liberto>
<URL/>
<Facebook-ID/>
</CONTACT>
</CONTACTS>
How do I merge these two XSLT files as a single XSLT file to get my final XML output.
how do i proceed with this? because there are two different xml files of similar type.
I'm using Eclipse Hellios run as -->XSL transformation to see the output.
Performing a chain of transformations is used quite often in XSLT applications, though doing this entirely in XSLT 1.0 requires the use of the vendor-specific xxx:node-set()
function. In XSLT 2.0 no such extension is needed as the infamous RTF datatype is eliminated there.
Here is an example (too-simple to be meaningful, but illustrating completely how this is done):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:apply-templates select="/*/*"/>
</xsl:variable>
<xsl:variable name="vPass1"
select="ext:node-set($vrtfPass1)"/>
<xsl:apply-templates mode="pass2"
select="$vPass1/*"/>
</xsl:template>
<xsl:template match="num[. mod 2 = 1]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="num" mode="pass2">
<xsl:copy>
<xsl:value-of select=". *2"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<nums>
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
the wanted, correct result is produced:
<num>2</num>
<num>6</num>
<num>10</num>
<num>14</num>
<num>18</num>
Explanation:
In the first step the XML document is transformed and the result is defined as the value of the variable
$vrtfPass1
. This copies only thenum
elements that have odd value (not even).The
$vrtfPass1
variable, being of type RTF, is not directly usable for XPath expressions so we convert it to a normal tree, using the EXSLT (implemented by most XSLT 1.0 processors) functionext:node-set
and defining another variable --$vPass1
whose value is this tree.We now perform the second transformation in our chain of transformations -- on the result of the first transformation, that is kept as the value of the variable
$vPass1
. Not to mess with the first-pass template, we specify that the new processing should be in a named mode, called "pass2". In this mode the value of anynum
element is multiplied by two.
XSLT 2.0 solution (no RTFs):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vPass1" >
<xsl:apply-templates select="/*/*"/>
</xsl:variable>
<xsl:apply-templates mode="pass2"
select="$vPass1/*"/>
</xsl:template>
<xsl:template match="num[. mod 2 = 1]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="num" mode="pass2">
<xsl:copy>
<xsl:value-of select=". *2"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You can use xsl:import
to reuse your XSLT files and then use the technique explained in the @Dimitre's answer as follows:
<xsl:stylesheet version="1.0"
xmlns:exslt="http://exslt.org/common"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="exslt">
<xsl:import href="phase1.xsl"/>
<xsl:import href="phase2.xsl"/>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="intermediate">
<xsl:apply-templates select="/CONTACTS/CONTACT" mode="phase1"/>
</xsl:variable>
<CONTACTS>
<xsl:apply-templates select="exslt:node-set($intermediate)"
mode="phase2"/>
</CONTACTS>
</xsl:template>
</xsl:stylesheet>
Where:
- phase1.xsl and phase2.xsl are your two xslt transforms
transforms are slightly modified adding a
mode
to each template. For instance, phase1.xsl transform:<xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*" mode="phase1"/> </xsl:copy> </xsl:template> <xsl:template match="CONTACT" mode="phase1"> <xsl:copy> <Customer-ID> <xsl:value-of select="generate-id(.)"/> </Customer-ID> <xsl:copy-of select="FirstName|LastName|URL"/> <Facebook-ID> <xsl:choose> <xsl:when test="URL"> <xsl:value-of select="substring-after(URL,'?id=')"/> </xsl:when> <xsl:otherwise> </xsl:otherwise> </xsl:choose> </Facebook-ID> <EMAILS> <xsl:apply-templates select="EMail" mode="phase1"/> </EMAILS> </xsl:copy> </xsl:template> <xsl:template match="EMail" mode="phase1"> <EMail> <Type><xsl:value-of select="substring-before( substring-after(.,'@'), '.')"/> </Type> <Value><xsl:value-of select="."/></Value> </EMail> </xsl:template>
For phase2.xsl you will use `mode="phase2" obviously.
When the above conditions are satisfied, and the merging transform is applied to your first input XML, the following output is obtained:
<CONTACTS>
<CONTACT>
<Customer-ID>d0e2</Customer-ID>
<FirstName>Ford</FirstName>
<LastName>Pasteur</LastName>
<gmail/>
<yahoo>pasteur.ford@yahoo.com</yahoo>
<liberto/>
<URL/>
<Facebook-ID/>
</CONTACT>
<CONTACT>
<Customer-ID>d0e9</Customer-ID>
<FirstName>Jack</FirstName>
<LastName>Sully</LastName>
<gmail/>
<yahoo/>
<liberto/>
<URL>http://www.facebook.com/profile.php?id=1000474277</URL>
<Facebook-ID>1000474277</Facebook-ID>
</CONTACT>
<CONTACT>
<Customer-ID>d0e16</Customer-ID>
<FirstName>Colombo</FirstName>
<LastName>Chao</LastName>
<gmail/>
<yahoo/>
<liberto>chao.colombo@liberto.it</liberto>
<URL/>
<Facebook-ID/>
</CONTACT>
</CONTACTS>
精彩评论