<a id='a1' name='a1'/>
<b text='b1'/>
<d test='test0' location='L0' text='c0'/>
<a id='a2' name='a2'/>
<b text='b2'/>
<c test='test1' location='L1' text='c1'/>
<c test='test2' location='L2' text='c2'/>
<a id='a3' name='a3'>
<b text='b3'/>
<c test='test3' location='L3' text='c3'/>
<c test='test4' location='L4' text='c4'/>
<c test='test5' location='L5' text='c5'/>
These elements are all siblings.some have no <c> element, I will do nothing with such elements.
For these have one or two or more <c> elements, I want to display a/@name only once for each <a> element。I apply a template like this, but it does not work:
<xsl:template match="a">
<xsl:choose>
<xsl:when test="following-sibling::c[1]">
<p>
<u>
<xsl:value-of select="(preceding-sibling::a[1])/@name"/>
</u>
</p>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
I want a output like this:
a2:
location:L1
test:test1
text:c1
location:L2
test:test2
text:c2
a3:
location:L3
test:test3
text:c3
location:L4
test:test4
text:c4
location:L5
test:test5
text:c5
From looking at your XSLT and expected results, it looks like that for each a element in your XML, you want to output infomation on the following c elements present, if any occur before the next a element present.
For this, you could use an xsl:key to look up c elements for a given a element
i.e. Group together all c elements by the first preceding a element.
Then, you can firstly select a elements for which there are such c elements like so:
Then, within this template, you can select the cc elements for output, like so:
So, given the following XSLT
When applied to the following XML
The following is output
Note that I am output the attributes on the c element in the order they appear in the XML document.