Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display XML Reference with XSL

My XML File:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
    <?xml-stylesheet type="text/xsl" href="bookstorex.xsl"?>
    <company>
        <bookstore>
            <standort1 standort-id="h_1"></standort1>
            <standort2 standort-id="h_2"></standort2>
        </bookstore>
        <book_list>
            <book category="cooking" book-id="b_1" standort-id="h_1">
                <title lang="en">Everyday Italian</title>
                <author>Giada De Laurentiis</author>
                <year>2005</year>
                <price>30.00</price>
            </book>
            <book category="children" book-id="b_2" standort-id="h_2">
                <title lang="en">Harry Potter</title>
                <author>J K. Rowling</author>
                <year>2005</year>
                <price>29.99</price>
            </book>
        </book_list>
    </company>

This is the XSL im using at the moment:

  <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="book_list">
            <html>
                <body>
                    <h1>Mein Bookstore</h1>
                    <table border = "2">
                        <tr>
                            <th>Bookname</th>
                            <th>Author</th>
                            <th>Year</th>
                            <th>price</th>
                        </tr>
                        <xsl:for-each select = "book">
                            <tr>
                                <td>
                                    <xsl:value-of select = "title"/>
                                </td>
                                <td>
                                    <xsl:value-of select = "author"/>
                                </td>
                                <td>
                                    <xsl:value-of select = "year"/>
                                </td>
                                <td>
                                    <xsl:value-of select = "price"/>
                                </td>
                            </tr>
                        </xsl:for-each>
                    </table>
                </body>
            </html>
        </xsl:template>
    </xsl:stylesheet>

Yo how can i display the books of standort1 inside a html table with XSLT?
At the moment im only able to simply display all books but i want to use id/idref to make a list for different bookstores

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use a key to resolve cross-references. Here’s an example:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="book-by-location" match="book" use="@standort-id"/>

<xsl:template match="/company">
    <html>
        <body>
            <xsl:for-each select="bookstore/*">
                <h1>
                    <xsl:text>Location </xsl:text>
                    <xsl:value-of select="@standort-id"/>
                </h1>
                <table border="2">
                    <tr>
                        <th>Bookname</th>
                        <th>Author</th>
                        <th>Year</th>
                        <th>price</th>
                    </tr>
                    <xsl:for-each select="key('book-by-location', @standort-id)">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td>
                                <xsl:value-of select="author"/>
                            </td>
                            <td>
                                <xsl:value-of select="year"/>
                            </td>
                            <td>
                                <xsl:value-of select="price"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading