XSLT拆分输出文件-Muenchian分组 [英] XSLT split output files - muenchian grouping

查看:76
本文介绍了XSLT拆分输出文件-Muenchian分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XSLT文件,以便转换大量数据.我想添加拆分"功能,作为链接的XSLT或在当前的XSLT内,它可以创建多个输出文件,以便将文件的大小限制在一定阈值以下. 假设输入的XML如下:

I have an XSLT file so as to transform large amount of data. I would like to add a "split" functionality, either as a chained XSLT or within the current XSLT that can create multiple output files so as to limit the size of the files under a certain threshold. Let's assume that the input XML is as below:

<People>
<Person>             
<name>John</name>             
<date>June12</date>             
<workTime taskID="1">34</workTime>             
<workTime taskID="2">12</workTime>             
</Person>             
<Person>             
<name>John</name>             
<date>June13</date>             
<workTime taskID="1">21</workTime>             
<workTime taskID="2">11</workTime>             
</Person>
<Person>             
<name>Jack</name>             
<date>June19</date>             
<workTime taskID="1">20</workTime>             
<workTime taskID="2">30</workTime>             
</Person>    
</People>

使用muenchian分组的XSLT文件如下所示.

The XSLT file is as below using muenchian grouping.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="PersonTasks" match="workTime" use="concat(@taskID, ../name)"/>
<xsl:template match="/">
    <People>
    <xsl:apply-templates select="//workTime[generate-id() = generate-id(key('PersonTasks',concat(@taskID, ../name))[1])]"/>
    </People>
</xsl:template>

<xsl:template match="workTime">
    <xsl:variable name="taskID">
        <xsl:value-of select="@taskID"/>
    </xsl:variable>
    <xsl:variable name="name">
        <xsl:value-of select="../name"/>
    </xsl:variable>
    <Person>
        <name>
            <xsl:value-of select="$name"/>
        </name>
        <taskID>
            <xsl:value-of select="$taskID"/>
        </taskID>
        <xsl:for-each select="//workTime[../name = $name][@taskID = $taskID]">
            <workTime>
                <date>
                    <xsl:value-of select="../date"/>
                </date>
                <time>
                    <xsl:value-of select="."/>
                </time>
            </workTime>
        </xsl:for-each>
    </Person>
</xsl:template>
</xsl:stylesheet>

但是,我希望将以下几个文件作为输出,而不是一个大文件.对于此示例,我为每个文件仅设置了一个名称..但这应该是一个参数.

However, I'd like ,as an output, several files as below instead of a large one. For this example, I have set only one name per file..but this should be a parameter.

第一人称输出文件:

<People>
    <Person>
        <name>John</name>
        <taskID>1</taskID>
        <workTime>
        <date>June12</date>
        <time>34</time>
        </workTime>
        <workTime>
        <date>June13</date>
        <time>21</time>
        </workTime>
    </Person>
    <Person>
        <name>John</name>
        <taskID>2</taskID>
        <workTime>
        <date>June12</date>
        <time>12</time>
        </workTime>
        <workTime>
        <date>June13</date>
        <time>11</time>
        </workTime>
    </Person>
</People>

第二人称输出文件:

<People>
    <Person>
        <name>Jack</name>
        <taskID>1</taskID>
        <workTime>
        <date>June19</date>
        <time>20</time>
        </workTime>
    </Person>
    <Person>
        <name>Jack</name>
        <taskID>2</taskID>
        <workTime>
        <date>June19</date>
        <time>30</time>
        </workTime>
    </Person>
</People>

使用XSLT 1.0首选和最优雅的方法是什么?有没有一种方法可以在XSLT中调用链接的XSLT来拆分输出文件?

What would be the preferred and most elegant approach using XSLT 1.0? Is there a way to call a chained XSLT within the XSLT so as to split the output files?

干杯.

推荐答案

有没有一种方法可以调用链接的XSLT 在XSLT中,以便拆分 输出文件?

Is there a way to call a chained XSLT within the XSLT so as to split the output files?

几种方法:

  1. 您可以编写扩展功能来做到这一点-查看XSLT处理器的文档.

  1. You could write an extension function to do this -- check the documentation of your XSLT processor.

使用 <exsl:document> 扩展元素EXSLT ,如果您的XSLT处理器支持的话

Use the <exsl:document> extension element of EXSLT, in case this is supported by your XSLT processor

使用 <saxon:output> 扩展元素(如果您有Saxon 6.x

Use the <saxon:output> extension element if you have Saxon 6.x

在您的编程语言中循环调用一个单独的转换,将要为其生成结果的人员的姓名作为参数传递给它.

In a loop from your programming language invoke a separate transformation, passing to it as parameter the name of the person for which to produce results.

以下是上面2和3的代码示例:

使用<saxon:output>:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:saxon="http://icl.com/saxon"
  extension-element-prefixes="saxon" >

 <xsl:template match="/">
  <xsl:for-each select="/*/*[not(. > 3)]">
   <saxon:output href="c:\xml\doc{.}">
    <xsl:copy-of select="."/>
   </saxon:output>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

在以下XML文档上应用此转换时:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

使用所需的内容创建三个文件:c:\xml\doc1c:\xml\doc2c:\xml\doc3.

three files: c:\xml\doc1 , c:\xml\doc2 and c:\xml\doc3 are created with the wanted contents.

使用<exslt:document>的相同示例:

The same example using <exslt:document>:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
  extension-element-prefixes="saxon" >

 <xsl:template match="/">
  <xsl:for-each select="/*/*[not(. > 3)]">
   <ext:document href="c:\xml\doc{.}">
    <xsl:copy-of select="."/>
   </ext:document>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

这篇关于XSLT拆分输出文件-Muenchian分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆