我需要将序列号 /MainHeader/Record/POSN 重新分配为 8001,8002....当我们在字段 /VAR 中有两个具有相同值的段时,如果 /Child-->段存在于 /Record 下,则必须将相同的序列号复制到 /Child 内的字段 /POSN。我的 XSLT 默认为所有记录生成序列号。
附有示例输入和输出。
输入:
<?xml version='1.0' encoding='UTF-8'?>
<MainHeader>
<Record SEGMENT="1">
<POSN>1000</POSN>
<VAR>1234</VAR>
<field>41</field>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>2000</POSN>
<VAR>45678</VAR>
<field>41</field>
<Child SEGMENT="1">
<POSN>2000</POSN>
<Union>UN</Union>
</Child>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>2000</POSN>
<VAR>45678</VAR>
<field>42</field>
<Child SEGMENT="1">
<POSN>2000</POSN>
<Union>UN</Union>
</Child>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>3000</POSN>
<VAR>778899</VAR>
<field>43</field>
<Child SEGMENT="1">
<POSN>3000</POSN>
<Union>UN</Union>
</Child>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>4000</POSN>
<VAR>778899</VAR>
<field>44</field>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>5000</POSN>
<VAR>118899</VAR>
<field>45</field>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
</MainHeader>
** 期望输出:**
<?xml version='1.0' encoding='UTF-8'?>
<MainHeader>
<Record SEGMENT="1">
<POSN>1000</POSN>
<VAR>1234</VAR>
<field>41</field>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>8001</POSN>
<VAR>45678</VAR>
<field>41</field>
<Child SEGMENT="1">
<POSN>8001</POSN>
<Union>UN</Union>
</Child>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>8002</POSN>
<VAR>45678</VAR>
<field>42</field>
<Child SEGMENT="1">
<POSN>8002</POSN>
<Union>UN</Union>
</Child>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>8003</POSN>
<VAR>778899</VAR>
<field>43</field>
<Child SEGMENT="1">
<POSN>8003</POSN>
<Union>UN</Union>
</Child>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>8004</POSN>
<VAR>778899</VAR>
<field>44</field>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
<Record SEGMENT="1">
<POSN>5000</POSN>
<VAR>118899</VAR>
<field>45</field>
<Subsegment SEGMENT="1">
<Qua>value1</Qua>
</Subsegment>
<Subsegment SEGMENT="1">
<Qua>Value2</Qua>
</Subsegment>
</Record>
</MainHeader>
** 我使用的 XSLT 如下:**
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="segments-by-field" match="Record" use="POSN"/>
<xsl:template match="/">
<MainHeader>
<xsl:for-each-group select="MainHeader/Record" group-by="POSN">
<xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
</MainHeader>
</xsl:template>
<xsl:template match="Record">
<Record>
<POSN><xsl:value-of select="8000 + position()"/></POSN>
<xsl:copy-of select="node()"/>
</Record>
</xsl:template>
</xsl:stylesheet>
请检查一次。
我认为以下 XSLT 3.0 样式表可以完成这项工作:
这不是一项简单的任务,我怀疑是否有简单的解决方案。如果您希望编号在各组中连续,则对记录进行分组无济于事。
或许可以尝试类似这样的方法:
XSLT 2.0