XSLT 2.0 |一个输入 --> 多个样式表 --> 多个输出

XSLT 2.0 | One Input --> Multiple stylesheets --> Multiple outputs

设置:撒克逊 HE 9.6 | XSLT 2.0

main.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:import href="first.xsl" />
    <xsl:import href="second.xsl" />


    <xsl:template match="/">

        <xsl:result-document href="output/first.xml" method="xml"> 
                <xsl:apply-imports/>  // <-- apply first.xsl           
        </xsl:result-document>

        <xsl:result-document href="output/second.xml" method="xml"> 
                <xsl:apply-imports/> // <-- apply second.xsl  
        </xsl:result-document>

    </xsl:template>

</xsl:stylesheet>

first.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <feedNumberOne>
            <xsl:apply-templates select="products/p[akt = '1' and export='tak']"/>
        </feedNumberOne>    
     </xsl:template>


    <xsl:template match="p">
        <first>
            <product id="{./code}" name="{./front_name}"/>
        </first>
    </xsl:template>
</xsl:stylesheet>

second.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <feedNumberTwo>
            <xsl:apply-templates select="products/p[akt = '1' and export='tak']"/>
        </feedNumberTwo>
    </xsl:template>


    <xsl:template match="p">
        <second>
            <product id="{./code}" name="{./front_name}"/>
        </second>
    </xsl:template>
</xsl:stylesheet>

Q : How to have 1 input, many xslt stylesheets imported with many xml output files in a single transformation


输入:

<?xml version="1.0" encoding="UTF-8"?>
<products>
   <p>
      <code>1030037</code>
      <front_name>Zelmer</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1030811</code>
      <front_name>Sony</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>900142</code>
      <front_name>Severin</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1030144</code>
      <front_name>Ubisoft</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1029390</code>
      <front_name>Blanco</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1029750</code>
      <front_name>Franke</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1028728</code>
      <front_name>WD</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1030916</code>
      <front_name>Electrolux</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031097</code>
      <front_name>High Sierra</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1030125</code>
      <front_name>Magnat</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031195</code>
      <front_name>Curver</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1031061</code>
      <front_name>High Sierra</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1029787</code>
      <front_name>Franke</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031450</code>
      <front_name>Spokey</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1029699</code>
      <front_name>Franke</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1029130</code>
      <front_name>Samsung</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1028754</code>
      <front_name>Technaxx</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1028822</code>
      <front_name>Netgear</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1030923</code>
      <front_name>Electrolux</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1031318</code>
      <front_name>Black&amp;Decker</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031625</code>
      <front_name>Russell Hobbs</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1031664</code>
      <front_name>HMS</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031627</code>
      <front_name>Russell Hobbs</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1031680</code>
      <front_name>Nikwax</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031899</code>
      <front_name>Wiko Mobile</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031758</code>
      <front_name>DeLonghi</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1032377</code>
      <front_name>MSI</front_name>
      <export>nie</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1031779</code>
      <front_name>DeLonghi</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1032865</code>
      <front_name>Samsung</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031848</code>
      <front_name>American Tourister</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1032617</code>
      <front_name>AMD</front_name>
      <export>nie</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1031923</code>
      <front_name>Dyson</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1032998</code>
      <front_name>Pioneer</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1032002</code>
      <front_name>Vivanco</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1032104</code>
      <front_name>Sony</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1032358</code>
      <front_name>Adidas</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1032741</code>
      <front_name>Samsonite</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1032928</code>
      <front_name>SanDisk</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1030230</code>
      <front_name>Printe</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1032287</code>
      <front_name>Netis</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1031984</code>
      <front_name>Krusell</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1029857</code>
      <front_name>Franke</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1030681</code>
      <front_name>Printe</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1032886</code>
      <front_name>Alcatel</front_name>
      <export>tak</export>
      <akt>1</akt>
   </p>
   <p>
      <code>1032688</code>
      <front_name>HP</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
   <p>
      <code>1028743</code>
      <front_name>Acer</front_name>
      <export>tak</export>
      <akt>0</akt>
   </p>
</products>

正如@MartinHonnen 所说,经典的解决方案是使用模式。

XSLT 3.0 在 xsl:stylesheet 上引入了 default-mode 属性,这使得这更容易一些。


编辑@Piotr Dajlido - 下面的解决方案

Note the usage of mode attribute in the first.xsl

main.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">

    <xsl:import href="first.xsl"  />
    <xsl:import href="second.xsl" />

    <xsl:output indent="yes"/>

    <xsl:template match="/">

        <xsl:result-document href="output/first.xml" method="xml"> 

            <xsl:call-template name="first">
            </xsl:call-template>
        </xsl:result-document>

        <xsl:result-document href="output/second.xml" method="xml">
            <xsl:call-template name="second"/>
        </xsl:result-document>

    </xsl:template>

</xsl:stylesheet>

first.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes"/>

    <xsl:template match="/" name="first">
        <feedNumberOne>
            <xsl:apply-templates select="products/p[akt = '1' and export='tak']" mode="first"/>
        </feedNumberOne> 
     </xsl:template>

    <xsl:template match="p" mode="first">
        <first>
            <product id="{./code}" name="{./front_name}"/>
        </first>
    </xsl:template>
</xsl:stylesheet>