This question is kind of related to my earlier question XSLT 1.0: using EXSLT to get element name according to substring I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<GenericRecs>
<GenericRecord>
<record>
<MBH1/>
</record>
<record>
<BAL1/>
</record>
<record>
<MBH2/>
</record>
<record>
<BAL2/>
</record>
<record>
<PAY2/>
</record>
<record>
<MBH3/>
</record>
<record>
<BAL3/>
</record>
<record>
<PAY3/>
</record>
</GenericRecord>
</GenericRecs>
and I would like to get this output:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<Card>
<Data>MBH1</Data>
<Data>BAL1</Data>
</Card>
<Card>
<Data>MBH2</Data>
<Data>BAL2</Data>
<Data>PAY2</Data>
</Card>
<Card>
<Data>MBH3</Data>
<Data>BAL3</Data>
<Data>PAY3</Data>
</Card>
</list>
With the help of Tomalak have come up with the following XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<list>
<xsl:apply-templates select="//record/*[starts-with(name(), 'MBH')]"/>
</list>
</xsl:template>
<xsl:template match="//record/*[starts-with(name(), 'MBH')]">
<Card>
<xsl:copy/>
</Card>
</xsl:template>
</xsl:stylesheet>
But that only gives me this output:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<Card>
<MBH1/>
</Card>
<Card>
<MBH2/>
</Card>
<Card>
<MBH3/>
</Card>
</list>
It is easy to uniquely identify the <MBH>
elements and list them.
I have tried to also work with a key that matches //record/*[not(starts-with(name(), 'MBH'))]
but that of course selects all of the other records not related to the previous <MBH>
.
Is it even possible to get the wanted output with that structure?
How could XSL know the (varying) amount of elements to the next <MBH>
element?