xmlxsltqnames

An attribute whose value must be a QName had the value ''


I have an XSL file that keeps coming up with the above error. Here is my code:

<?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"/>
    <xsl:template match="faculty">
        <xsl:element name='{fname}'>
            <xsl:for-each select="students/name">
                <name>
                    <xsl:value-of select="."/>
                </name>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

And here is part of my source XML file:

    <faculties xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xsi:noNamespaceSchemaLocation='257lab2a.xsd'>
        <faculty>
            <fname>literal arts</fname>
            <students>
                <name>ron dell</name>
                <mark>52</mark>
                <phone number='349-095-9867'></phone>
                <courseCategory category='full time'></courseCategory>
                <courseNo courseNumber='LART433'></courseNo>
            </students>
        </faculty>
    </faculties>

Solution

  • It appears to be because fname contains text which is not a valid element name (can't contain a space). Try the following, but keep in mind there could other cases you need to address, depending on your data.

    <xsl:template match="faculty">
        <xsl:element name="{translate(fname, ' ', '_')}">
            <xsl:for-each select="students/name">
                <name>
                    <xsl:value-of select="."/>
                </name>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>