一个网络服务调用返回给我一个 1000 个工人的 xml。如何使用 XSLT 将文件拆分为多个 xml 文件,每个文件包含 50 个工人?

AdityaGG

我有一个 xml 文件,它有 1000 个工人,xpath env:Envelope/env:Body/wd:Get_Workers_Response/wd:Response_Data/wd:Worker

我需要使用 XSLT 根据工作人员的数量(例如 50)将文件拆分为多个文件

我的网络服务响应:

        <?xml version="1.0" encoding="utf-8"?>
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
        <env:Body>
            <wd:Get_Workers_Response xmlns:wd="urn:com.workday/bsvc" wd:version="v35.1">
                <wd:Response_Filter>
                    <wd:As_Of_Effective_Date>2021-01-04</wd:As_Of_Effective_Date>
                    <wd:As_Of_Entry_DateTime>2021-01-04T23:28:03.113-08:00</wd:As_Of_Entry_DateTime>
                    <wd:Page>1</wd:Page>
                    <wd:Count>100</wd:Count>
                </wd:Response_Filter>
                <wd:Response_Results>
                    <wd:Total_Results>2</wd:Total_Results>
                    <wd:Total_Pages>1</wd:Total_Pages>
                    <wd:Page_Results>2</wd:Page_Results>
                    <wd:Page>1</wd:Page>
                </wd:Response_Results>
                <wd:Response_Data>
                    <wd:Worker>
                        <wd:Worker_Data>
                            <wd:Worker_ID>1</wd:Worker_ID>
                            <wd:Name>aaaaaa</wd:Name>
                        </wd:Worker_Data>
                    </wd:Worker>
                    <wd:Worker>
                        <wd:Worker_Data>
                            <wd:Worker_ID>2</wd:Worker_ID>
                            <wd:Name>bbbbbb</wd:Name>
                        </wd:Worker_Data>
                    </wd:Worker>
                    <wd:Worker>
                        <wd:Worker_Data>
                            <wd:Worker_ID>3</wd:Worker_ID>
                            <wd:Name>cccccc</wd:Name>
                        </wd:Worker_Data>
                    </wd:Worker>

.....
..... 998 more workers

期望输出:

文件 1:工人 1 至 50

                    <wd:Worker>
                        <wd:Worker_Data>
                            <wd:Worker_ID>1</wd:Worker_ID>
                            <wd:Name>aaaaaa</wd:Name>
                        </wd:Worker_Data>
                    </wd:Worker>
                    <wd:Worker>
                        <wd:Worker_Data>
                            <wd:Worker_ID>2</wd:Worker_ID>
                            <wd:Name>bbbbbb</wd:Name>
                        </wd:Worker_Data>
                    </wd:Worker>
...
...
                    <wd:Worker>
                        <wd:Worker_Data>
                            <wd:Worker_ID>50</wd:Worker_ID>
                            <wd:Name>xxxxx</wd:Name>
                        </wd:Worker_Data>
                    </wd:Worker>

文件 2:工人 51 至 100 ......

我的 XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wd="urn:com.workday/bsvc"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:template match="wd:Response_Data">
        <xsl:for-each-group select="wd:Worker" group-adjacent="(position()-1) idiv 50">
            <xsl:result-document href="batch{position()}.xml">
                <batch nr="{position()}">
                    <xsl:copy-of select="current-group()"/>
                </batch>
            </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>
    
</xsl:stylesheet>
迈克尔·凯

在 XSLT 2.0+ 中

<xsl:template match="wd:Response_Data">
  <xsl:for-each-group select="wd:Worker" group-adjacent="(position()-1) idiv 50">
    <xsl:result-document href="batch{position()}.xml">
      <batch nr="{position()}">
        <xsl:copy-of select="current-group()"/>
      </batch>
    </xsl:result-document>
  </xsl:for-each-group>
</xsl:template>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章