kacmak7
kacmak7

Reputation: 151

XSLT: how to combine 2 xml files to one table

I need to create HTML from XSLT file. The problem is that I don't know how to manage with 2 XMLs. I tried something like below.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:b="http://www.demo.com" xmlns:a="http://www.demo.com/author" 
xmlns:p="http://www.demo.com/person" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
etc ... 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/" name="xsl:initial-template">

    <html>
        <body>
            <xsl:variable name="bookFile" select="document('file:///C:/Users/Kacper Makuch/Desktop/library/book.xml')"></xsl:variable>
            <xsl:variable name="authorFile" select="document('file:///C:/Users/Kacper Makuch/Desktop/library/author.xml')"></xsl:variable>

            <h1>Book Collection</h1>

            <table border="4">
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Author</th>
                </tr>

                <xsl:for-each select="$bookFile//b:book">
                <xsl:variable name="idBook" select="@id"></xsl:variable>
                <tr>
                    <td><xsl:value-of select="$idBook"/></td>
                    <!--<td><xsl:value-of select="@id"/></td>-->
                    <td><xsl:value-of select="name"/></td>
                    <td><xsl:value-of 
select="$authorFile//a:author[bookId=$idBook]/p:lastname"/></td>
                </tr>
                </xsl:for-each>         
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

In the output there are correctly iterated books from xml with correct IDs, but without their names and p:lastnames of authors.

Upvotes: 0

Views: 138

Answers (2)

kacmak7
kacmak7

Reputation: 151

I've managed this problem. The problem was with prefixes, namely in my xml input files I've got defined default namespace (with no prefix) and namespace with prefix with the same URI. It's proper, but in XSLT, XPath expression must have prefix. I've omitted prefix and that's why my output was empty. Code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:b="http://www.demo.com" xmlns:a="http://www.demo.com/author" 
xmlns:p="http://www.demo.com/person" xmlns:xs="http://www.w3.org/2001/XMLSchema" etc
<xsl:template match="/" name="xsl:initial-template">

    <html>
        <body>
            <xsl:variable name="bookFile" select="document('file:///C:/Users/Kacper 
Makuch/Desktop/library/book.xml')"></xsl:variable>
            <xsl:variable name="authorFile" select="document('file:///C:/Users/Kacper 
Makuch/Desktop/library/author.xml')"></xsl:variable>

            <h1>Book Collection</h1>

            <table border="4">
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Author</th>
                </tr>

                <xsl:for-each select="$bookFile//b:book">
                <xsl:variable name="idBook" select="@id"></xsl:variable>
                <tr>
                    <td><xsl:value-of select="$idBook"/></td>
                    <td><xsl:value-of select="b:name"/></td>
                    <td><xsl:value-of 
select="concat($authorFile//a:author[a:bookId=$idBook]/p:firstname, ' ', 
$authorFile//a:author[a:bookId=$idBook]/p:lastname)"/></td>
                </tr>
                </xsl:for-each>         
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

Upvotes: 0

muck
muck

Reputation: 141

I have a solution where it works like this:

<xsl:variable name="IdToAdd" select="document('D:/EXPORT/FOLDER/File.xml')/Satz/@id"></xsl:variable>

Have you tried without file:/// and without whitespaces?

Upvotes: 1

Related Questions