如何在XSLT 2.0中对数字排序?

时间:2019-04-08 14:25:34

标签: xslt xslt-2.0

我有以下输入XML

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
    <id>1234</id>
    <loc>New York</loc>
    <Days>1</Days>
    <StartDate>2019-01-26</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <Batch>A</Batch>
    <Days>3</Days>
    <Units>2</Units>
    <StartDate>2019-02-01</StartDate>
</Worker>
</Workers>

我的代码如下所示

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

 <xsl:template match="Worker[Batch]">
 <xsl:variable name="currElem" select="."/>
 <xsl:for-each select="1 to Days">
  <xsl:apply-templates select="$currElem" mode="replicate">
    <xsl:with-param name="dayNo" select="."/>
  </xsl:apply-templates>
</xsl:for-each>
<xsl:for-each select="1 to Units">
  <xsl:apply-templates select="$currElem" mode="replicate">
    <xsl:with-param name="dayNo" select="."/>
  </xsl:apply-templates>
</xsl:for-each>
</xsl:template>

<xsl:template match="Worker" mode="replicate">
<xsl:param name="dayNo"/>
<Worker>
  <xsl:sequence select="id, loc"/>
  <RecordNumber><xsl:value-of select="$dayNo"/></RecordNumber>
  <WorkerDays>1</WorkerDays>
  <StartDate>
    <xsl:value-of select="xs:date(StartDate) + xs:dayTimeDuration('P1D') * 
($dayNo - 1)"/>
  </StartDate>
</Worker>
</xsl:template>

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

当前输出为

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
    <id>1234</id>
    <loc>New York</loc>
    <Days>1</Days>
    <StartDate>2019-01-26</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>1</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-01</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>2</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-02</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>3</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-03</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>1</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-01</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>2</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-02</StartDate>
</Worker>
 </Workers>

我需要<RecordNumber></RecordNumber>才能显示顺序编号。当前显示的是1、2、3和1、2。

相反,我需要1,2,3,4,5。

当前,我的XSLT代码基于<for-each><Days>的值执行两个<Units>循环(请参考示例输入xml中的第二个<Worker>节点) 。我认为这就是为什么我在<RecordNumber>

中得到1,2,3和1,2的原因

我尝试使用位置position()并将其声明为变量并在模板中使用它,这似乎不起作用。

我还尝试了另一个for-each循环来执行position(),这似乎也不起作用

预期输出为

<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
    <id>1234</id>
    <loc>New York</loc>
    <Days>1</Days>
    <StartDate>2019-01-26</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>1</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-01</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>2</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-02</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>3</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-03</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>4</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-01</StartDate>
</Worker>
<Worker>
    <id>2345</id>
    <loc>Boston</loc>
    <RecordNumber>5</RecordNumber>
    <WorkerDays>1</WorkerDays>
    <StartDate>2019-02-02</StartDate>
</Worker>
 </Workers>

有人可以帮我找出我还能尝试获得正确序列号的其他方法吗(请<RecordNumber>

1 个答案:

答案 0 :(得分:2)

只需定义一个变量来保存Days的值,然后在复制dayNo时将其添加到Units参数上

<xsl:variable name="days" select="Days" />
<xsl:for-each select="1 to Units">
  <xsl:apply-templates select="$currElem" mode="replicate">
    <xsl:with-param name="dayNo" select=". + $days"/>
  </xsl:apply-templates>
</xsl:for-each>