将多个XSLT文件复制到2个不同xml文件的单个XSLT文件 [英] Multiple XSLT files to single XSLT file for 2 different xml files
问题描述
这是我的xml文件:
<?xml version =1.0encoding =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>电子邮件
< / CONTACT>
< / CONTACTS>
我以下使用XSLT文件作为我的第一个版本的xml输出。
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =xmlindent =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:否则>
< / xsl:否则>
< / xsl:choose>
< / Facebook-ID>
< EMAILS>
< xsl:apply-templates select =EMail/>
< / EMAILS>
< / xsl:copy>
< / xsl:template>
< xsl:template match =EMail>
<电子邮件>
< Type>< xsl:value-of select =substring-before(
substring-after(。,'@'),
'。)/>
< / Type>
< Value>< xsl:value-of select =。/>< / Value>
< / EMail>
< / xsl:template>
< / xsl:stylesheet>
我从上述XSLT文件的第一个版本的xml输出:
<?xml version =1.0encoding =windows-1250?>
< CONTACTS>
< CONTACT>
< Customer-ID> N65539< / Customer-ID>
< FirstName> Ford< / FirstName>
< LastName&Pasteur< / LastName>
< EMAILS>
<电子邮件>
< 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>
<电子邮件>
< Type> liberto< / Type>
< Value> chao.colombo@liberto.it< / Value>
< / EMail>
< / EMAILS>
< / CONTACT>
< / CONTACTS>
这是我的第二个XSLT文件:
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =xmlindent =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>
这是我最后的xml输出从第二个XSLT文件:
<?xml version =1.0encoding =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< / 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>
如何将这两个XSLT文件合并为一个XSLT文件,以获得最终的XML输出。 / p>
我该如何处理?因为有两个类似的xml文件类型。
我使用Eclipse Hellios运行为 - > XSL转换来查看输出。
您可以使用 xsl:import
重用您的XSLT文件,然后使用@ Dimitre的答案如下:
< 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 =xmlindent =yes/>
< xsl:strip-space elements =*/>
< xsl:template match =/>
< xsl:variable name =intermediate>
< xsl:apply-templates select =/ CONTACTS / CONTACTmode =phase1/>
< / xsl:variable>
< CONTACTS>
< xsl:apply-templates select =exslt:node-set($ intermediate)
mode =phase2/>
< / CONTACTS>
< / xsl:template>
< / xsl:stylesheet>
其中:
- phase1.xsl和phase2.xsl是您的两个xslt变换
-
转换稍加修改,将
模式
添加到每个模板。例如,phase1.xsl转换:
code>< xsl:template match =node()| @ *>
< xsl:copy>
< xsl:apply-templates select =node()| @ *mode =phase1/>
< / xsl:copy>
< / xsl:template>
< xsl:template match =CONTACTmode =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:否则>
< / xsl:否则>
< / xsl:choose>
< / Facebook-ID>
< EMAILS>
< xsl:apply-templates select =EMailmode =phase1/>
< / EMAILS>
< / xsl:copy>
< / xsl:template>
< xsl:template match =EMailmode =phase1>
<电子邮件>
< Type>< xsl:value-of select =substring-before(
substring-after(。,'@'),
'。)/>
< / Type>
< Value>< xsl:value-of select =。/>< / Value>
< / EMail>
< / xsl:template>
对于phase2.xsl,您将使用mode =phase2。
当满足上述条件,并且变换应用于您的第一个输入XML,获得以下输出:
< 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>
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</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.
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>
这篇关于将多个XSLT文件复制到2个不同xml文件的单个XSLT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!