为每个重复的节点创建新的xml

时间:2019-09-17 10:24:00

标签: xml xslt

我有一个类似下面的xml。我将创建3个具有相同结构的新xml,并且应为每个eamployee标签重复相同的结构。请参阅下面的完整xml和结果。我只给empnum 1001放一个xml,但是我要3个xml

<Syncemployee releaseID="9.2">
<ApplicationArea>
    <Sender>
        <LogicalID>infor.database.connectiontest</LogicalID>
        <ComponentID>External</ComponentID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2019-09-17T09:51:03.996Z</CreationDateTime>
    <BODID>infor.database.connectiontest:1568713863992:74:0</BODID>
</ApplicationArea>
<DataArea>
    <Sync>
        <TenantID>infor.test.connection</TenantID>
        <AccountingEntityID/>
        <LocationID/>
        <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
        </ActionCriteria>
    </Sync>
    <employee>
        <empnum>1001</empnum>
        <fullname>John Doe                                </fullname>
        <dateofhire>2009-08-28</dateofhire>
        <lastdaterecord>2019-09-17T11:28:20.287</lastdaterecord>
    </employee>
    <employee>
        <empnum>1002</empnum>
        <fullname>Jane Doe                                </fullname>
        <dateofhire>2009-08-28</dateofhire>
        <lastdaterecord>2019-09-17T11:28:20.287</lastdaterecord>
    </employee>
    <employee>
        <empnum>1003</empnum>
        <fullname>Tim Bone                                </fullname>
        <dateofhire>2014-01-01</dateofhire>
        <lastdaterecord>2019-09-17T11:28:20.287</lastdaterecord>
    </employee>
</DataArea>
</Syncemployee>

我想为每个重复的员工标签创建一个新的xml,如下所示

<Syncemployee releaseID="9.2">
<ApplicationArea>
    <Sender>
        <LogicalID>infor.database.connectiontest</LogicalID>
        <ComponentID>External</ComponentID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2019-09-17T09:51:03.996Z</CreationDateTime>
    <BODID>infor.database.connectiontest:1568713863992:74:0</BODID>
</ApplicationArea>
<DataArea>
    <Sync>
        <TenantID>infor.test.connection</TenantID>
        <AccountingEntityID/>
        <LocationID/>
        <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
        </ActionCriteria>
    </Sync>
    <employee>
        <empnum>1001</empnum>
        <fullname>John Doe                                </fullname>
        <dateofhire>2009-08-28</dateofhire>
        <lastdaterecord>2019-09-17T11:28:20.287</lastdaterecord>
    </employee>
</DataArea>
</Syncemployee>

所以我想要上面的结果为empnum 1001、1002和1003,实际上我想拥有3个xml

亲爱的马丁,您的代码不适用于我的xml 这是您转型的结果

<?xml version="1.0" encoding="UTF-8"?>


        infor.database.connectiontest
        External
        OnError

    2019-09-17T09:51:03.996Z
    infor.database.connectiontest:1568713863992:74:0



        infor.test.connection






    <Syncemployee>
<ApplicationArea>
    <Sender>
        <LogicalID>infor.database.connectiontest</LogicalID>
        <ComponentID>External</ComponentID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2019-09-17T09:51:03.996Z</CreationDateTime>
    <BODID>infor.database.connectiontest:1568713863992:74:0</BODID>
</ApplicationArea>
<DataArea>
    <Sync>
        <TenantID>infor.test.connection</TenantID>
        <AccountingEntityID/>
        <LocationID/>
        <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
        </ActionCriteria>
    </Sync>
    <employee>
        <empnum>1001</empnum>
        <fullname>John Doe                                </fullname>
        <dateofhire>2009-08-28</dateofhire>
        <lastdaterecord>2019-09-17T11:28:20.287</lastdaterecord>
    </employee>


</DataArea>
</Syncemployee>
    <Syncemployee>
<ApplicationArea>
    <Sender>
        <LogicalID>infor.database.connectiontest</LogicalID>
        <ComponentID>External</ComponentID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2019-09-17T09:51:03.996Z</CreationDateTime>
    <BODID>infor.database.connectiontest:1568713863992:74:0</BODID>
</ApplicationArea>
<DataArea>
    <Sync>
        <TenantID>infor.test.connection</TenantID>
        <AccountingEntityID/>
        <LocationID/>
        <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
        </ActionCriteria>
    </Sync>

    <employee>
        <empnum>1002</empnum>
        <fullname>Jane Doe                                </fullname>
        <dateofhire>2009-08-28</dateofhire>
        <lastdaterecord>2019-09-17T11:28:20.287</lastdaterecord>
    </employee>

</DataArea>
</Syncemployee>
    <Syncemployee>
<ApplicationArea>
    <Sender>
        <LogicalID>infor.database.connectiontest</LogicalID>
        <ComponentID>External</ComponentID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2019-09-17T09:51:03.996Z</CreationDateTime>
    <BODID>infor.database.connectiontest:1568713863992:74:0</BODID>
</ApplicationArea>
<DataArea>
    <Sync>
        <TenantID>infor.test.connection</TenantID>
        <AccountingEntityID/>
        <LocationID/>
        <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
        </ActionCriteria>
    </Sync>


    <employee>
        <empnum>1003</empnum>
        <fullname>Tim Bone                                </fullname>
        <dateofhire>2014-01-01</dateofhire>
        <lastdaterecord>2019-09-17T11:28:20.287</lastdaterecord>
    </employee>
</DataArea>
</Syncemployee>

1 个答案:

答案 0 :(得分:0)

如果在一个结果中重复几次“与原始结构相同”,那么您将不再拥有格式良好的文档,因为您将拥有多个根元素,而在XSLT转换结果中可能是这样,不寻常的结果。如果您真的想要那种结果,那么从另一个线程执行的方法中,您要做的就是明确地开始处理

    <xsl:template match="/">
        <xsl:apply-templates select="//employee"/>
    </xsl:template>

当然也删除了xsl:result-document的使用,因此整个代码看起来像

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//employee"/>
    </xsl:template>

    <xsl:template match="employee">
        <xsl:apply-templates select="/" mode="split">
            <xsl:with-param name="this-split-element" select="." tunnel="yes"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="@* | node()" mode="split">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()" mode="#current"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template _match="employee" mode="split">
        <xsl:param name="this-split-element" tunnel="yes"/>
        <xsl:if test=". is $this-split-element">
            <xsl:next-match/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bwdwrE