【问题标题】:Create multiple files in XSLT在 XSLT 中创建多个文件
【发布时间】:2014-03-18 21:18:26
【问题描述】:

我的 xml 文件

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

【问题讨论】:

    标签: xml xslt-2.0


    【解决方案1】:

    这是一种方法:

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

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2013-01-23
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2014-04-23
      相关资源
      最近更新 更多