Home > Blockchain >  XSLT How to print all attributes inside an element: <node id="" name="" rando
XSLT How to print all attributes inside an element: <node id="" name="" rando

Time:09-14

The only answers I come across is how to list the value of the node and child nodes itself. What I would like to know is how I can print all the values inside the node tag itself.

XML (there can be more books within catalog)

<data>
<info>
    <catalog Id="1111" Locale="en_GB">
        <books>
            <book id="01" Name="Title 1" Author="John Doe"></book>
            <book id="02" Name="Title 2" Author="Jane Doe"></book>
        </books>

What I currently have:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body> 

<xsl:for-each select="data/info/catalog">
   <xsl:value-of select="@Id" />
   <xsl:value-of select="/books/book/@Name"/>
</xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>

Specifically, I would like the output to be:

Catalog Id: 1111

Attributes:
id: 01
Name: Title 1
Author: John Doe

id: 02
Name: Title 2
Author: Jane Doe

The reason it's being done this way because I have dozens upon dozens of values and it's easier to fit them inside the node tag itself instead of creating hundreds of sub-sub-subchilds.

CodePudding user response:

You did not post the exact HTML code you want to have. Try something like:

XSLT 1.0

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

<xsl:template match="/data">
    <html>
        <body> 
            <xsl:for-each select="info/catalog">
                <xsl:text>Catalog Id: </xsl:text>
                <xsl:value-of select="@Id" />
                <br/>
                <br/>
                <xsl:text>Attributes:</xsl:text>
                <br/>
                <xsl:for-each select="books/book">
                    <xsl:for-each select="@*">
                        <xsl:value-of select="name()" />
                        <xsl:text>: </xsl:text>
                        <xsl:value-of select="." />
                        <br/>
                    </xsl:for-each>
                    <br/>
                </xsl:for-each> 
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>
  • Related