XSLT:根据分组输出到多个xml文件中

发布于 2024-10-02 06:34:11 字数 2450 浏览 4 评论 0原文

假设您有下面的 xml。目标是按 FirstName 分组并将 Person 导出到不同的 xml 文件中。每个输出 xml 文件最多只能包含 X 个不同名字。

下面是 X = 3 XML 输入的所需转换示例

<People>
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Doe</LastName> 
    </Person> 
    <Person>             
        <FirstName>Jack</FirstName>             
        <LastName>White</LastName> 
    </Person>
    <Person>             
        <FirstName>Mark</FirstName>             
        <LastName>Wall</LastName> 
    </Person>
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Ding</LastName> 
    </Person> 
    <Person>             
        <FirstName>Cyrus</FirstName>             
        <LastName>Ding</LastName> 
    </Person>  
    <Person>             
        <FirstName>Megan</FirstName>             
        <LastName>Boing</LastName> 
    </Person>
</People>          

XML 输出 1 具有 3 个不同的 FirstName

<People>
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Doe</LastName> 
    </Person> 
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Ding</LastName> 
    </Person>
    <Person>             
        <FirstName>Jack</FirstName>             
        <LastName>White</LastName> 
    </Person>
    <Person>             
        <FirstName>Mark</FirstName>             
        <LastName>Wall</LastName> 
    </Person>  
</People> 

XML 输出 2 具有 2 个剩余的 FirstName >

<People>
    <Person>             
        <FirstName>Cyrus</FirstName>             
        <LastName>Ding</LastName> 
    </Person>  
    <Person>             
        <FirstName>Megan</FirstName>             
        <LastName>Boing</LastName> 
    </Person>
</People> 

在我看来, muenchian 分组可以与 一起使用来生成多个输出文件。然而,核心问题是,在导出到新文件之前,我们可以在哪里设置人数阈值?

Let's assume, you have the xml below. The goal is to group by FirstName and export the Person into different xml files. Each output xml files should only contain up to X different FirstName.

Below is an example of the desired transformation with X = 3

XML input:

<People>
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Doe</LastName> 
    </Person> 
    <Person>             
        <FirstName>Jack</FirstName>             
        <LastName>White</LastName> 
    </Person>
    <Person>             
        <FirstName>Mark</FirstName>             
        <LastName>Wall</LastName> 
    </Person>
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Ding</LastName> 
    </Person> 
    <Person>             
        <FirstName>Cyrus</FirstName>             
        <LastName>Ding</LastName> 
    </Person>  
    <Person>             
        <FirstName>Megan</FirstName>             
        <LastName>Boing</LastName> 
    </Person>
</People>          

XML output 1 with 3 different FirstName

<People>
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Doe</LastName> 
    </Person> 
    <Person>             
        <FirstName>John</FirstName>             
        <LastName>Ding</LastName> 
    </Person>
    <Person>             
        <FirstName>Jack</FirstName>             
        <LastName>White</LastName> 
    </Person>
    <Person>             
        <FirstName>Mark</FirstName>             
        <LastName>Wall</LastName> 
    </Person>  
</People> 

XML output 2 with the 2 remaining FirstName

<People>
    <Person>             
        <FirstName>Cyrus</FirstName>             
        <LastName>Ding</LastName> 
    </Person>  
    <Person>             
        <FirstName>Megan</FirstName>             
        <LastName>Boing</LastName> 
    </Person>
</People> 

It seems to me that the muenchian grouping can be used along with the to produce multiple output files. However, the core question is where we can set a threshold in number of person before exporting to a new file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2024-10-09 06:34:11

下面是使用 XSLT 2.0 分两步执行此操作的示例:

<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:param name="n" as="xs:integer" select="3"/>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="People">
    <xsl:variable name="groups" as="element(group)*">
      <xsl:for-each-group select="Person" group-by="FirstName">
        <group>
          <xsl:copy-of select="current-group()"/>
        </group>
      </xsl:for-each-group>
    </xsl:variable>
    <xsl:for-each-group select="$groups" group-by="(position() - 1) idiv $n">
      <xsl:result-document href="group{position()}.xml">
        <People>
          <xsl:copy-of select="current-group()"/>
        </People>
      </xsl:result-document>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

我稍后可能会尝试转换为 XSLT 1.0 和 EXSLT。

[编辑]
以下是转换为 XSLT 1.0 和 EXSLT 的尝试:

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

  <xsl:param name="n" select="3"/>

  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="person-by-firstname" 
           match="Person"
           use="FirstName"/>

  <xsl:template match="People">
    <xsl:variable name="groups">
      <xsl:for-each select="Person[generate-id() = generate-id(key('person-by-firstname', FirstName)[1])]">
        <group>
          <xsl:copy-of select="key('person-by-firstname', FirstName)"/>
        </group>
      </xsl:for-each>
    </xsl:variable>
    <xsl:for-each select="exsl:node-set($groups)/group[(position() - 1) mod $n = 0]">
      <exsl:document href="groupTest{position()}.xml">
        <People>
          <xsl:copy-of select="Person | following-sibling::group[position() < $n]/Person"/>
        </People>
      </exsl:document>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Here is an example of doing it in two steps with XSLT 2.0:

<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:param name="n" as="xs:integer" select="3"/>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="People">
    <xsl:variable name="groups" as="element(group)*">
      <xsl:for-each-group select="Person" group-by="FirstName">
        <group>
          <xsl:copy-of select="current-group()"/>
        </group>
      </xsl:for-each-group>
    </xsl:variable>
    <xsl:for-each-group select="$groups" group-by="(position() - 1) idiv $n">
      <xsl:result-document href="group{position()}.xml">
        <People>
          <xsl:copy-of select="current-group()"/>
        </People>
      </xsl:result-document>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

I might try to convert to XSLT 1.0 and EXSLT later.

[edit]
Here is an attempt to translate into XSLT 1.0 and EXSLT:

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

  <xsl:param name="n" select="3"/>

  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="person-by-firstname" 
           match="Person"
           use="FirstName"/>

  <xsl:template match="People">
    <xsl:variable name="groups">
      <xsl:for-each select="Person[generate-id() = generate-id(key('person-by-firstname', FirstName)[1])]">
        <group>
          <xsl:copy-of select="key('person-by-firstname', FirstName)"/>
        </group>
      </xsl:for-each>
    </xsl:variable>
    <xsl:for-each select="exsl:node-set($groups)/group[(position() - 1) mod $n = 0]">
      <exsl:document href="groupTest{position()}.xml">
        <People>
          <xsl:copy-of select="Person | following-sibling::group[position() < $n]/Person"/>
        </People>
      </exsl:document>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文