Looping through all attributes of a XML element in XSLT

Posted by TheGNUGuy on Stack Overflow See other posts from Stack Overflow or by TheGNUGuy
Published on 2010-06-17T13:51:05Z Indexed on 2010/06/17 13:53 UTC
Read the original article Hit count: 259

Filed under:
|
|
|

Hey everyone,

I am trying to use <xsl:for-each select="@*"> to grab all the attributes of a given element but when i do that my <xsl:choose> statement doesn't execute.

Here is the element that I'm working with:
<textBox id="Airfare" value="" label="text 1"/>

Here is the XSLT template I'm using:

<xsl:template match="textBox">
<div>
    <xsl:choose>
        <xsl:when test="@label">
            <xsl:value-of select="@label"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>No Label Defined</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
<xsl:element name="input">
    <xsl:attribute name="type">text</xsl:attribute>
    <xsl:for-each select="@*">
        <xsl:choose>
            <xsl:when test="@id">
                <xsl:attribute name="name">form_<xsl:value-of select="@id"/></xsl:attribute>
                <xsl:attribute name="id">form_<xsl:value-of select="@id"/></xsl:attribute>
            </xsl:when>
            <xsl:when test="@label">
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="current()"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:element>
</div>

And when I generate the HTML using PHP I get this:

<div>text 1<input type="text" id="Airfare" value="" label="text 1"></div>

As you can see it didn't add form_ to the id attribute it didn't generate a name attribute and it didn't skip over the label attribute.

Thanks for your help!

© Stack Overflow or respective owner

Related posts about html

Related posts about Xml