开发者

Merge keys in two different XML files (separation of data from language)

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">
    <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>
</ht开发者_如何学编程ml>

Thanks in advance!


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">
<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜