tags:

views:

36

answers:

1

I have two XML files to render one page in XSLT. This is because I have to separate the language from the data for a multilingual website. I need to relationate data from one and other to print a value.

My index.xml:

<?xml version="1.0" encoding="utf-8"?>
<index>
    <language>en</language>

    <example>
        <category id="1">
            <href>/category/id/1</href>
        </category>
        <category id="2">
            <href>/category/id/2</href>
        </category>
    </example>
</index>

Then I've a base.en.xml that looks like:

<?xml version="1.0" encoding="utf-8"?>
<language>
    <category id="1">Category 1</category>
    <category id="2">Category 2</category>
</language>

My incomplete index.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:param name="language" select="document('index.en.xml'))" /> 

    <xsl:template match="/">
        <html>
            <head>
                <title>Example</title>    
            </head>

            <body>
                <ul>
                    <xsl:apply-templates select="index/example/category" />
                </ul>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="index/example/category">
        <a href="{href}"></a>
    </xsl:template>

</xsl:stylesheet>

Finally the desired output:

<html>
    <head>
        <title>Example</title>
    </head>

    <body>
        <ul>
            <li><a href="/category/id/1">Category 1</a></li>
            <li><a href="/category/id/2">Category 2</a></li>
        </ul>
    </body>
</html>

Thanks in advance!

+2  A: 

Your document() function call in the xsl:param had an extra ")" that was breaking your XSLT.

Once that is resolved, you can execute XPATH expressions against the language param.

$language/language/category[current()/@id=@id]

Inside of your index/example/category template, current() refers to the currently matched index/example/category element. The predicate filter uses it's @id to select the correct /language/category element.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output indent="yes" />

    <xsl:param name="language" select="document('index.en.xml')" />

    <xsl:template match="/">
        <html>
            <head>
                <title>Example</title>
            </head>

            <body>
                <ul>
                    <xsl:apply-templates select="index/example/category" />
                </ul>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="index/example/category">
        <a href="{href}"><xsl:value-of select="$language/language/category[current()/@id=@id]"/></a>
    </xsl:template>

</xsl:stylesheet>
Mads Hansen
@Mads Hansen: Thank you! It works fine! One question: why do you use indent="yes"?Edit: thank you for the nice explanation!
Isern Palaus
You can take that out. I usually add it when I'm testing my transforms to make the output easier to read. It adds extra "bloat" (file size, length), so you might not want to have it set to yes on a production system.
Mads Hansen