XSLT:如何将2个xml文件合并到一个表中

时间:2018-07-18 06:57:21

标签: html xml database xslt html-table

我需要从XSLT文件创建HTML。问题是我不知道如何使用2个XML进行管理。我尝试了以下类似的方法。

<?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>

在输出中,从XML正确地迭代了带有正确ID的书,但没有它们的名称和作者的p:lastname

2 个答案:

答案 0 :(得分:1)

我有一个这样的解决方案:

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

您是否尝试了没有file:///和空格?

答案 1 :(得分:0)

我已经解决了这个问题。问题出在前缀,即在我的xml输入中 档案 我已经定义了默认名称空间(无前缀)和带前缀的名称空间 与 相同的URI。正确,但是在XSLT中,XPath表达式必须具有前缀。我有 省略 前缀,这就是为什么我的输出为空。代码:

<?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>