访问命名空间中 XML 中的元素

Acessing elements in XML that are in a namespace

提供名称空间前缀以将 XML 转换为 XSLT 已在 SO 中广泛介绍。参见例如XSLT Transform XML with Namespaces, XSLT with XML source that has a default namespace set to xmlns, and XSLT Transformating XML to XML。然而,经过无数小时的研究和反复试验,我可以说我没能成功。

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<library xmlns="http://void.net/library/1.0">
    <catalog>
        <cd id="c1">
            <singer id="s1">
                <name>Kate</name>
                <surname>Apple</surname>
            </singer>
        <title>Great CD</title>
        </cd>
        <cd id="c2">
            <singer id="s2">
                <name>Mary</name>
                <surname>Orange</surname>
            </singer>
        <title>Even better CD</title>
        </cd>
    </catalog>
</library>

这就是我到目前为止得出的结论:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    xmlns:b="http://void.net/library/1.0"
    exclude-result-prefixes="b">
    <xsl:output method="text" indent="no" />

    <xsl:template match="/b:library">
        <xsl:text>singer,title
        </xsl:text>
        <xsl:for-each select="b:catalog/b:cd">
            <xsl:value-of select="concat(b:singer/b:name, ' ', b:singer/b:surname, ', ', b:title, '
')" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

这里是example and error list

我修正了样式表中的拼写错误并想出了这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:b="http://void.net/library/1.0" exclude-result-prefixes="b">
    <xsl:output method="text" indent="no" />

    <xsl:template match="/b:library">
        <xsl:text>singer,title&#xa;</xsl:text>
        <xsl:for-each select="b:catalog/b:cd">
            <xsl:value-of select="concat(b:singer/b:name, ' ', b:singer/b:surname, ', ', b:title, '&#xa;')" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输出:

singer,title
Kate Apple, Great CD
Mary Orange, Even better CD

所以一切似乎都按预期工作。