我有这个 xml 文件:
<A>
<B>
<elt>Add</elt>
...there are some element here
<bId>2</bId>
</B>
<B>
<elt>Add</elt>
...there are some element here
<bId>2</bId>
</B>
<B>
<elt>Add</elt>
...there are some element here
<bId>2</bId>
</B>
<B>
<elt>can</elt>
...there are some element here
<bId>3</bId>
</B>
<B>
<elt>can</elt>
...there are some element here
<bId>3</bId>
</B>
我想检查每个bId元素的值。如果这个值与前面或后面的bId元素相同,那么我会将 bloc B的其他元素放在另一个 bloc中,但转换后将被拒绝的元素bId除外。为了让您理解我的问题,这是预期的输出:
<CA>
<cplx>
<spRule>
<elt>Add</elt>
...
</spRule>
<spRule>
<elt>Add</elt>
...
</spRule>
<spRule>
<elt>Add</elt>
...
</spRule>
</cplx>
<cplx>
<spRule>
<elt>can</elt>
...
</spRule>
<spRule>
<elt>can</elt>
...
</spRule>
</cplx>
</CA>
即使当 xml 文件中的元素未按 bId的值排序时,我也希望获得相同的预期输出。我尝试使用这个 xsl 代码:
<xsl:for-each select="bId"
<CA>
<cplx>
<xsl:choose>
<xsl:when test="node()[preceding::bId]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:when>
</cplx>
</CA>
</xsl:for-each>
但它不会走路。有人可以帮我吗?谢谢