1

I have an xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Groups>
    <Group num="1">
        <GroupName>First Group</GroupName>
    </Group>
     <Group num="2">
        <GroupName>Second Group</GroupName>
    </Group>
</Groups>

Would like to display the GroupName's as a drop down menu in XSLT.

I tried the following and it shows empty drop down menu.

<SELECT NAME="Groups">                      
    <xsl:for-each select="Groups/Group">                            
        <OPTION VALUE="{GroupName}">
            <xsl:value-of select="GroupName"/>
        </OPTION>                           
    </xsl:for-each>
</SELECT> 

1 Answer 1

1

It depends on your context. You didn't show the template where that for-each is operating. If it doesn't match the root node / it won't find the relative nodes and will be empty.

It will work with this stylesheet:

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

    <xsl:template match="/">
        <SELECT NAME="Groups">                      
            <xsl:for-each select="Groups/Group">                            
                <OPTION VALUE="{GroupName}">
                    <xsl:value-of select="GroupName"/>
                </OPTION>                           
            </xsl:for-each>
        </SELECT> 
    </xsl:template>

</xsl:stylesheet>

If it didn't work in your case, your match attribute is probably different. It's all a matter of context. If your template matches Group, then your for-each has to consider that context. This will also work:

<xsl:template match="Groups">
    <SELECT NAME="Groups">                      
        <xsl:for-each select="Group">                            
            <OPTION VALUE="{GroupName}">
                <xsl:value-of select="GroupName"/>
            </OPTION>                           
        </xsl:for-each>
    </SELECT> 
</xsl:template>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. My changes worked when I removed the template to the form. Earlier, I declared everything in a separate template and called it in the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.