I have this xml file:
<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>
I want to check the value of each bId element. If this value is the same with the preceding or following bId element, then I will put the other elements of the bloc B in another bloc excepted the element bId which will be rejected after the transformation. To make my question understanding by you, here is the expected output:
<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>
Even though when the element in the xml file are not sorted by the value of bId, I want also get the same expected output.
I try to use this xsl code:
<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>
but it doesn’t walk. Could someone help me please?
Thanks
Assuming your first description to group adjacent elements with the same
bIdvalue is what you want an XSLT 1.0 way is as follows:If you simply want to group all
Belements with the samebIdvalue then use