如何使用XSLT 2,0循环X次?

时间:2019-01-25 17:54:06

标签: xml xslt-2.0

在需要您的帮助以使用XSLT 2.0转换XML时,我有以下要求。

    单个工作人员的
  1. <Workers>节点需要插入的时间与工作人员<Days>的总数一样多,例如如果某个工作人员的总天数为5,则<Workers><Worker>必须存在5次以保存该工作人员的数据。
  2. 每次为一个工人插入新节点时,<StartDate>需要增加一天,并将其值映射到新元素WorkerStartDate

  3. 每次创建新节点时,都需要插入一个新元素<RecordNumber>来保存该循环的值。

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
    <Worker>
        <id>1234</id>
        <loc>New York</loc>
        <Days>5</Days>
        <StartDate>2019-02-01</StartDate>
    </Worker>
</Workers>

XSLT转换后的预期输出XML

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>1</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-02</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>2</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-03</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>3</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-04</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>4</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-05</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>5</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-06</WorkerStartDate>
    </Worker>
</Workers>

我能够获得以下输出<RecordNumber><WorkerStartDate>返回错误的数据

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>1</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-02</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>1</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-02</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>1</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-02</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>1</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-02</WorkerStartDate>
    </Worker>
</Workers>
<Workers>
    <Worker>
        <WorkerId>1234</WorkerId>
        <WorkerLoc>New York</WorkerLoc>
        <RecordNumber>1</RecordNumber>
        <WorkerDays>1</WorkerDays>
        <WorkerStartDate>2019-02-02</WorkerStartDate>
    </Worker>
</Workers>

我的XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:functx="http://www.functx.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
    <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="start" select="1"/>
        <xsl:variable name="counter" select="Workers/Worker/Days"/>
        <xsl:variable name="Records" select="Workers/Worker"/>
        <xsl:for-each select="$start to $counter">
            <xsl:apply-templates select="$Records" mode="replicate">
                <xsl:with-param name="data" select="."/>
            </xsl:apply-templates>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="Worker" mode="replicate">
        <xsl:param name="data"/>
        <Workers>
            <Worker>
                <WorkerId><xsl:value-of select="id"/></WorkerId>
                <WorkerLoc><xsl:value-of select="loc"/></WorkerLoc>
                <RecordNumber><xsl:value-of select="position()"/></RecordNumber>
                <WorkerDays>1</WorkerDays>
                <WorkerStartDate><xsl:value-of select="xs:date(StartDate) + xs:dayTimeDuration('P1D')"/>
                </WorkerStartDate>
            </Worker>
        </Workers>
    </xsl:template>

</xsl:stylesheet>

当前输出XML存在以下问题:

  1. <WorkerStartDate>始终返回1,根据XML元素,WorkerStartDate预计将增加一天
  2. <RecordNumber>始终返回1,其中RecordNumber期望增加1,例如1,2,3。。
  3. 这不是什么大问题-<WorkerDays>必须始终返回1。现在,我对该值进行了硬编码。在循环结束之前,不确定是否有一种有效的方法将1打印为值。

有人可以帮助我解决我遇到的问题吗?

1 个答案:

答案 0 :(得分:1)

您已经定义了名为data的参数,该参数绑定到您使用for-each表达式在to中处理的整数值,因此您可以简单地将该参数与

结合使用
<RecordNumber><xsl:value-of select="$data"/></RecordNumber>

<WorkerStartDate>
  <xsl:value-of select="xs:date(StartDate) + xs:dayTimeDuration('P1D') * $data"/>
</WorkerStartDate>

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