代码之家  ›  专栏  ›  技术社区  ›  Jon W

基于不同值的枚举值XSLT 2.0

  •  4
  • Jon W  · 技术社区  · 17 年前

    我有一长串带有命名标识符的XML值。我需要为每个不同的标识符创建单独的输出文件,这些标识符分组在一起并具有唯一的名称。

    例如,假设我有:

    <List>
       <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
          Hello World!
       </Item>
       <Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
          Goodbye World!
       </Item>
       <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
          This example text should be in the first file
       </Item>
       <Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
          This example text should be in the second file
       </Item>
       <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
          Hello World!
       </Item>
    </List>
    

    我如何编写一个转换(XSLT 2.0)来输出这些分组为生成的文件名并具有唯一值的文件?例如:映射第一个 @group 到file1.xml和第二个

    1 回复  |  直到 17 年前
        1
  •  3
  •   Dimitre Novatchev    17 年前

    下面是一个使用XSLT 2.0中一些好的新特性的解决方案:

    这种转变 :

    <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
          <!--                                                  --> 
        <xsl:template match="/*">
          <xsl:variable name="vTop" select="."/>
          <!--                                                  --> 
            <xsl:for-each-group select="Item" group-by="@group">
              <xsl:result-document href="file:///C:/Temp/file{position()}.xml">
                <xsl:element name="{name($vTop)}">
                  <xsl:copy-of select="current-group()"/>
                </xsl:element>
              </xsl:result-document>
            </xsl:for-each-group>
        </xsl:template>
    </xsl:stylesheet>
    

    <List>
        <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
             Hello World!
        </Item>
        <Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
              Goodbye World!
      </Item>
        <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
              This example text should be in the first file
     </Item>
        <Item group="::this_other_long_and_complicated_group_name_that_cannot_be_a_filename::">
              This example text should be in the second file
     </Item>
        <Item group="::this_long_and_complicated_group_name_that_cannot_be_a_filename::">
              Hello World!
      </Item>
    </List>
    

    生成所需的两个文件 : file1.xml和file2.xml