2014-03-18 31 views
0

我的XML文件在XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<tests> 
<testrun run="test1"> 
    <test name="foo" pass="true" /> 
    <test name="bar" pass="true" /> 
    <test name="baz" pass="true" /> 
</testrun> 
<testrun run="test2"> 
    <test name="foo" pass="true" /> 
    <test name="bar" pass="false" /> 
    <test name="baz" pass="false" /> 
     <testrun run="test2.1"> 
      <test name="foo" pass="true" /> 
      <test name="bar" pass="false" /> 
      <test name="baz" pass="false" /> 
     </testrun> 
</testrun> 
<testrun run="test3"> 
    <test name="foo" pass="false" /> 
    <test name="bar" pass="true" /> 
    <test name="baz" pass="false" /> 
</testrun> 
</tests> 

我的XSL文件

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

<xsl:output method="text"/> 
<xsl:output method="html" indent="yes" name="html"/> 

<xsl:template match="/"> 
<xsl:for-each select="//testrun"> 
    <xsl:variable name="filename" 
    select="concat('output1/',@run,'.html')" /> 
    <xsl:value-of select="$filename" /> 
    <xsl:result-document href="{$filename}" format="html"> 
    <html><body> 
    <xsl:value-of select="@run"/> 
    </body></html> 
    </xsl:result-document> 
</xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

在輸出創建多個文件,我得到的四個文件(TEST1,TEST2,test2.1,TEST3)。是否有可能只創建三個文件(test1,test2,test3),其中test2包含值test2.1?

回答

0

下面是做這件事:

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

    <xsl:output method="text"/> 
    <xsl:output method="html" indent="yes" name="html"/> 

    <!-- There's no need for a for-each loop. Just match all the 
     testruns that are immediate children of tests --> 
    <xsl:template match="/tests/testrun"> 
    <xsl:variable name="filename" 
        select="concat('output1/',@run,'.html')" /> 
    <xsl:value-of select="$filename" /> 
    <xsl:result-document href="{$filename}" format="html"> 
     <html><body> 
     <xsl:value-of select="@run"/> 
     <!-- You need to adapt this to your specific needs. --> 
     <xsl:apply-templates select="test"/> 
     <xsl:apply-templates select="testrun"/> 
     </body></html> 
    </xsl:result-document> 
    </xsl:template> 

    <!-- These templates are here mostly for illustration 
     purposes. You'd have to decide that you actually want to do 
     with your data. --> 
    <xsl:template match="test"> 
    <p>test: <xsl:value-of select="@name"/></p> 
    </xsl:template> 

    <xsl:template match="testrun/testrun"> 
    <p>testrun: <xsl:value-of select="@run"/></p> 
    <xsl:apply-templates select="test"/> 
    </xsl:template> 
</xsl:stylesheet> 

output/test2.html文件將包含:

<html> 
    <body>test2 
     <p>test: foo</p> 
     <p>test: bar</p> 
     <p>test: baz</p> 
     <p>testrun: test2.1</p> 
     <p>test: foo</p> 
     <p>test: bar</p> 
     <p>test: baz</p> 
    </body> 
</html> 

(如果編輯test2.1測試來更改名稱,使他們不」重複那些test2,你將能夠清楚地看到test2.1之後的foo,bar,baz是test2.1的那些。)