0

I have file XML

<?xml version="1.0" encoding="utf-8"?>
<library xmlns="http://example.net/library/1.0">
    <books>
        <book id="b1">
            <author id="a1">
                <name>Henryk</name>
                <surname>Kowalski</surname>
                <born>1991-01-23</born>
            </author>
            <title>"Do okoła Ziemi"</title>
            <published>1993</published>
            <isbn>985-12-23-15489-23</isbn>
        </book>
        <book id="b2">
            <author id="a2">
                <name>Franek</name>
                <surname>Brzeczyszczykiewicz</surname>
                <born>1975-09-05</born>
                <died>1999-12-30</died>
            </author>
            <title>Jak rozpetałem II wojnę światową</title>
            <published>1968</published>
        </book>
    </books>
</library>

and file XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ns1="http://void.net/library/1.0">
    <xsl:output method="text" indent="no" />
    <xsl:template match="/books">
        <xsl:text>author,title,published
        </xsl:text>
        <xsl:for-each select="book">
            <xsl:value-of select="concat(author/name, ', ', author/surname, ', ', title, ', ', published, ' ')" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

I can not find the error. As a result, it gets the contents of the XML file, Not Defined Fields. Could someone tell me Where is the Error?

Anzor
  • 25
  • 5

1 Answers1

0

You were missing the namespace in your stylesheet.
All elements in the XML file are in the namespace xmlns="http://example.net/library/1.0". So you have to include it in all your XPath expressions in the XSL file.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ns1="http://void.net/library/1.0"
    xmlns:ns2="http://example.net/library/1.0">  <!-- namespace from XML file -->
    <xsl:output method="text" indent="no" />
    <xsl:template match="ns2:books">             <!-- removed root '/', because ns2:books is not root -->
        <xsl:text>author,title,published
        </xsl:text>
        <xsl:for-each select="ns2:book">
            <xsl:value-of select="concat(ns2:author/ns2:name, ', ', ns2:author/ns2:surname, ', ', ns2:title, ', ', ns2:published, '&#xa;')" /> 
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

I also added a newline instead of a space at the end of line.

zx485
  • 28,498
  • 28
  • 50
  • 59