xsltexslt

XSLT 1.0: using EXSLT to get element name according to substring


I have the following XML and I want to get only the element names that start with "MBH":

<?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>

I have the following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common"
version="1.0">

<xsl:variable name="x" select="ext:node-set(substring(local-name(//record/child::*),1,3)='MBH')"/>

<xsl:variable name="mbh">
    <xsl:for-each select="$x">
            <item>
                <xsl:copy>
                    <xsl:value-of select="local-name(.)"/>
                </xsl:copy> 
            </item>
    </xsl:for-each>
</xsl:variable>

<xsl:template match="/">
    <xsl:apply-templates select="$mbh"/>
</xsl:template>
</xsl:stylesheet>

But all I get is an error "Description: Can not convert #RTREEFRAG to a NodeList!" I am using EXSLT but I do not understand why I would get that error.


Solution

  • I have the following XML and I want to get only the element names that start with "MBH":

    What's wrong with

    <xsl:apply-templates select="//record/*[starts-with(name(), 'MBH')]" />
    

    ?

    A few notes: