【发布时间】:2021-02-12 05:21:31
【问题描述】:
亲爱的, 我在 xml 下面有一个 edi xml,里面有 2 个文档。我想为 M_810 下的每个 ST 和 SE 分配一个序列号 我的第一个 M_810 有 2 个 M_810 - ST-D_329 和 SE-D-329 - 我想要序列号 2001 第二个 M_810 - ST-D_329 和 SE-D_329 - 我想要序列号 2002
我试过下面的代码。它总是在需要的地方给我 2001。寻求解决此问题的帮助。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="D_329">
<xsl:if test="((../name() = 'S_ST') or (../name() = 'S_SE'))">
<D_329><xsl:value-of select="2000 + ../position()"/></D_329>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
src xml:
<?xml version='1.0' encoding='UTF-8'?><Interchange>
<M_810>
<S_ST>
<D_143>810</D_143>
<D_329>47857</D_329>
</S_ST>
<S_BIG>
<D_373>20210128</D_373>
<D_76>917183</D_76>
<D_373_2>20210120</D_373_2>
<D_324>13453796</D_324>
</S_BIG>
<S_SE>
<D_96>21</D_96>
<D_329>47857</D_329>
</S_SE>
</M_810>
<M_810>
<S_ST>
<D_143>810</D_143>
<D_329>47858</D_329>
</S_ST>
<S_BIG>
<D_373>20210128</D_373>
<D_76>917184</D_76>
<D_373_2>20210120</D_373_2>
<D_324>13453797</D_324>
</S_BIG>
<S_SE>
<D_96>21</D_96>
<D_329>47858</D_329>
</S_SE>
</M_810>
</Interchange>
需要的输出:
<?xml version="1.0" encoding="UTF-8"?><Interchange>
<M_810>
<S_ST>
<D_143>810</D_143>
<D_329>2001</D_329>
</S_ST>
<S_BIG>
<D_373>20210128</D_373>
<D_76>917183</D_76>
<D_373_2>20210120</D_373_2>
<D_324>13453796</D_324>
</S_BIG>
<S_SE>
<D_96>21</D_96>
<D_329>2001</D_329>
</S_SE>
</M_810>
<M_810>
<S_ST>
<D_143>810</D_143>
<D_329>2002</D_329>
</S_ST>
<S_BIG>
<D_373>20210128</D_373>
<D_76>917184</D_76>
<D_373_2>20210120</D_373_2>
<D_324>13453797</D_324>
</S_BIG>
<S_SE>
<D_96>21</D_96>
<D_329>2002</D_329>
</S_SE>
</M_810>
</Interchange>
父节点中每次出现都要修改的xml节点:
<S_ST>
<D_143>810</D_143>
<D_329>47857</D_329> >> <D_329>2001</D_329>
</S_ST>
and
<S_SE>
<D_96>21</D_96>
<D_329>47857</D_329> >> <D_329>2001</D_329>
</S_SE>
【问题讨论】: