Chaitany Ram
Chaitany Ram

Reputation: 141

XSLT for XML to HTML

Hi I am looking for an XSLT for generating HTML as shown below. Following are the details of the transformations. Please provide your inputs.

For each PageGroup I need to create an

In HTML an Li element should be populated.

    <li>
        <a>Test</a>
    </li>

And under each Pagegroup for every Page Ul should be populated.
<ul>
        <li>
            <a>Test Role 1</a>
        </li>
        <li>
            <a>Test Role 2</a>
        </li>
</ul>

XML:

<menuitems>
    <pagegroup title="Test">
        <pages id="1" url="Test1.aspx" description="Tes 1" type="1" role="Test Role 1"/>
        <pages id="2" url="Test2.aspx" description="Tes 2" type="1" role="Test Role 2"/>
        <pagegroup title="Projects">
            <pages id="4" url="Test 2 3.aspx" description="Test 2 3" type="1" role="Rol 3"/>
            <pages id="4" url="Test 2 5.aspx" description="Test 2 5" type="1" role="Rol 4"/>
        </pagegroup>
    </pagegroup>
</menuitems>

Output HTML expecting

<ul>
    <li>
        <a>Test</a>
    </li>
    <ul>
        <li>
            <a>Test Role 1</a>
        </li>
        <li>
            <a>Test Role 2</a>
        </li>
        <ul>
            <li>
                <a>Projects</a>
            </li>
            <ul>
                <li>
                    <a>Test 2 3</a>
                </li>
                <li>
                    <a>Test 2 5</a>
                </li>
            </ul>
        </ul>
    </ul>
</ul>

XSLT Tried

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
            <xsl:template match="pagegroup">
                        <ul>
                                    <xsl:apply-templates select="pages"/>
                                    <li>
                                    <xsl:value-of select="@title"/>
                                    </li>
                        </ul>
            </xsl:template>
            <xsl:template match="pages">
                        <li>
                                    <xsl:value-of select="@role"/>
                                    <xsl:if test="pagegroup">
                                                <ul>
                                                            <xsl:apply-templates select="pages"/>
                                                            <li>
                                                            <xsl:value-of select="@title" />
                                                            </li>
                                                </ul>
                                    </xsl:if>
                        </li>
            </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Views: 3007

Answers (2)

Woody
Woody

Reputation: 5130

you are a bit out on what you want I think.

This gets closer, but you still won't get what you want, as in one groiup of pages you seem to want role, and in another description. That is possible but I am not sure if that is what you want

    <?xml version="1.0" encoding="ISO-8859-1"?>

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

        <xsl:template match="pagegroup">
            <ul>
                <li>
                    <a><xsl:value-of select="@title"/></a>
                </li>
                <xsl:if test="pages | pagegroup">
                    <ul>
                        <xsl:apply-templates select="pages | pagegroup"/>
                    </ul>
                </xsl:if>

            </ul>
        </xsl:template>

        <xsl:template match="pages">
            <li>
                <a><xsl:value-of select="@role"/></a>
                <xsl:if test="pagegroup">
                    <ul>

                        <li>
                            <xsl:value-of select="@title" />
                        </li>
                    </ul>
                </xsl:if>
            </li>
        </xsl:template>
    </xsl:stylesheet>

Upvotes: 1

Tim C
Tim C

Reputation: 70618

You are not too far off. The main issue is that in your template to match pagegroup you are doing this...

<xsl:apply-templates select="pages"/>

When really you want to be doing this, to match both pages and pagegroups

<xsl:apply-templates/>

Try the following XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
   <xsl:template match="pagegroup">
      <ul>
         <li>
            <xsl:value-of select="@title"/>
         </li>
         <ul>
            <xsl:apply-templates/>
         </ul>
      </ul>
   </xsl:template>
   <xsl:template match="pages">
      <li>
         <xsl:value-of select="@role"/>
      </li>
   </xsl:template>
</xsl:stylesheet>

When applied to your XML, the following is output

<ul>
   <li>Test</li>
   <ul>
      <li>Test Role 1</li>
      <li>Test Role 2</li>
      <ul>
         <li>Projects</li>
         <ul>
            <li>Rol 3</li>
            <li>Rol 4</li>
         </ul>
      </ul>
   </ul>
</ul>

Upvotes: 2

Related Questions