当前位置 : 主页 > 网页制作 > xml >

多个XSLT文件到单个XSLT文件,用于2个不同的xml文件

来源:互联网 收集:自由互联 发布时间:2021-06-13
这是我的xml文件: ?xml version="1.0" encoding="windows-1250"?CONTACTS CONTACT FirstNameFord/FirstName LastNamePasteur/LastName EMailpasteur.ford@yahoo.com/EMail /CONTACT CONTACT FirstNameJack/FirstName LastNameSully/LastName URLhttp:
这是我的xml文件:

<?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>

我使用下面的XSLT文件作为我的拳头版本的xml输出.

<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>

我从上面的XSLT文件输出的第一个xml版本:

<?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>

这是我的第二个XSLT文件:

<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>

这是我从第二个XSLT文件输出的最终xml:

<?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>

如何将这两个XSLT文件合并为单个XSLT文件以获取最终的XML输出.

我该如何处理?因为有两种不同类型的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="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>

哪里:

> phase1.xsl和phase2.xsl是你的两个xslt变换
>转换略有修改,为每个模板添加模式.例如,phase1.xsl变换:

    
    

<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>

对于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>
网友评论