为什么不起作用(带XML的XSLT)

时间:2017-08-17 16:12:48

标签: xml xslt

我有文件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>

和文件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>

我找不到错误。因此,它获取XML文件的内容,Not Defined Fields。有人能告诉我错误在哪里?

1 个答案:

答案 0 :(得分:0)

您在样式表中缺少名称空间 XML文件中的所有元素都在命名空间xmlns="http://example.net/library/1.0"中。因此,您必须将它包含在XSL文件中的所有XPath表达式中。

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

我还在行尾添加了换行符而不是空格。