我需要根据一些属性对值进行分组并填充它。
输入XML在下面,重复的jobid被定义为(|)分隔符。
在生成实际的0/p时,我需要按jobid分组并生成重复jobid的序列号。我该怎么做呢?
<employes>
<job>
<jobid>125</jobid>
</job>
<job>
<jobid>1011</jobid>
</job>
<job>
<jobid>123|1</jobid>
</job>
<job>
<jobid>123|2</jobid>
</job>
<job>
<jobid>1010</jobid>
</job>
<job>
<jobid>123|1</jobid>
</job>
</employes>
预期结果:我需要根据Division对值进行分组并填充,即
<employes>
<job>
<jobnum>1</jobnum>
<jobid>123</jobid>
</job>
<job>
<jobnum>2</jobnum>
<jobid>123</jobid>
</job>
<job>
<jobnum>3</jobnum>
<jobid>123</jobid>
</job>
<job>
<jobid>125</jobid>
</job>
<job>
<jobid>1010</jobid>
</job>
<job>
<jobid>1011</jobid>
</job>
</employes>
由于您正在使用XSLT 2.0(问题最初仅标记为XSLT 2.0),您应该能够使用xsl:for-each-group
并按jobid
中|
之前的数字分组…
XML输入
<employes>
<job>
<jobid>125</jobid>
</job>
<job>
<jobid>1011</jobid>
</job>
<job>
<jobid>123|1</jobid>
</job>
<job>
<jobid>123|2</jobid>
</job>
<job>
<jobid>1010</jobid>
</job>
<job>
<jobid>123|1</jobid>
</job>
</employes>
XSLT 2.0 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="job" group-by="substring-before(concat(jobid,'|'),'|')">
<xsl:sort select="substring-before(concat(jobid,'|'),'|')" data-type="number" order="ascending"/>
<xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="job[contains(jobid,'|')]">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::jobid)]"/>
<jobnbr><xsl:value-of select="position()"/></jobnbr>
<jobid><xsl:value-of select="substring-before(jobid,'|')"/></jobid>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML输出
<employes>
<job>
<jobnbr>1</jobnbr>
<jobid>123</jobid>
</job>
<job>
<jobnbr>2</jobnbr>
<jobid>123</jobid>
</job>
<job>
<jobnbr>3</jobnbr>
<jobid>123</jobid>
</job>
<job>
<jobid>125</jobid>
</job>
<job>
<jobid>1010</jobid>
</job>
<job>
<jobid>1011</jobid>
</job>
</employes>