0

我正在使用http://www.freeformatter.com/xsl-transformer.html。这是我的 XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="listacd_es1.xslt"?>

<listacd>
    <artista>
        <nome>Stanley Jordan</nome>
        <albums>
            <album>
                <titolo>Magic Touch</titolo>
                <anno>1985</anno>
                <etichetta>Blue Note</etichetta>
            </album>
            <album>
                <titolo>Stolen Moments</titolo>
                <anno>1991</anno>
                <etichetta>Blue Note</etichetta>
            </album>
        </albums>
    </artista>
    <artista>
        <nome>Nick Drake</nome>
        <albums>
            <album>
                <titolo>Pink Moon</titolo>
                <anno>1972</anno>
                <etichetta>Island</etichetta>
            </album>
            <album>
                <titolo>Bryter Layter</titolo>
                <anno>1970</anno>
                <etichetta>Island</etichetta>
            </album>
            <album>
                <titolo>Five leaves left</titolo>
                <anno>1970</anno>
                <etichetta>Island</etichetta>
            </album>
        </albums>
    </artista>
    <artista>
        <nome>Jeff Buckley</nome>
        <albums>
            <album>
                <titolo>Grace</titolo>
                <anno>1994</anno>
                <etichetta>Columbia</etichetta>
            </album>
            <album>
                <titolo>Mistery white boy</titolo>
                <anno>2000</anno>
                <etichetta>Columbia</etichetta>
            </album>
        </albums>
    </artista>
    <artista>
        <nome>Joe Satriani</nome>
        <albums>
            <album>
                <titolo>Surfing with the alien</titolo>
                <anno>1987</anno>
                <etichetta>Epic</etichetta>
            </album>
            <album>
                <titolo>Not of this earth</titolo>
                <anno>1988</anno>
                <etichetta>Relativity</etichetta>
            </album>
        </albums>
    </artista>
</listacd>

这是 XSLT 文件:

<?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>
            <xsl:apply-templates />
        </html>
    </xsl:template>

    <xsl:template match="artista">
        <b>
            <xsl:value-of select="nome" /> :
        </b>
        <xsl:apply-templates />
        <br />
        <br />
    </xsl:template>

    <xsl:template match="album">
        <xsl:value-of select="titolo" />
    </xsl:template>

</xsl:stylesheet>

结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <b>Stanley Jordan :</b>
  Stanley JordanMagic TouchStolen Moments
  <br />
  <br />
  <b>Nick Drake :</b>
  Nick DrakePink MoonBryter LayterFive leaves left
  <br />
  <br />
  <b>Jeff Buckley :</b>
  Jeff BuckleyGraceMistery white boy
  <br />
  <br />
  <b>Joe Satriani :</b>
  Joe SatrianiSurfing with the alienNot of this earth
  <br />
  <br />
</html>

我知道结果不是很漂亮,我不太在意,但我不明白为什么艺术家的名字被写了 2 次。我该怎么做才能让每个艺术家的名字只出现一次?

4

1 回答 1

2

这部分与 XSLT 的内置模板有关。这些是在无法找到与样式表中的节点匹配的特定模板时使用的模板。内置模板将输出任何匹配的文本节点的文本,否则它将简单地跳过该节点并继续处理其子节点。

由于此模板而出现问题

<xsl:template match="artista">
    <b>
        <xsl:value-of select="nome" /> :
    </b>
    <xsl:apply-templates />
    <br />
    <br />
</xsl:template>

特别是<xsl:apply-templates />. 这将查找与当前Artista元素的子节点匹配的模板,在您的情况下是nomealbums,它们在您的 XSLT 中都没有匹配的模板。因此,内置模板适用,对于nome元素,将输出其中的文本,这是重复文本的来源。

有两种可能的简单解决方案。首先,您可以将此模板与您的 XSLT 匹配,以匹配您的XSLT中的名称,并忽略它,以防止应用内置模板:

 <xsl:template match="nome" />

第二种解决方案是<xsl:value-of select="." />Artista模板中删除 current,取而代之的是有一个模板匹配nom输出值。试试这个 XSLT 作为一个例子

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <xsl:apply-templates />
        </html>
    </xsl:template>

    <xsl:template match="artista">
        <xsl:apply-templates />
        <br />
        <br />
    </xsl:template>

    <xsl:template match="album">
        <xsl:value-of select="titolo" />
    </xsl:template>

    <xsl:template match="nome">
        <b>
            <xsl:value-of select="." /> :
        </b>
   </xsl:template>
</xsl:stylesheet>
于 2013-08-31T13:52:01.963 回答