Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml xsl:apply模板到底是如何工作的,为什么它要写两次名称?_Xml_Xslt_Apply Templates - Fatal编程技术网

Xml xsl:apply模板到底是如何工作的,为什么它要写两次名称?

Xml xsl:apply模板到底是如何工作的,为什么它要写两次名称?,xml,xslt,apply-templates,Xml,Xslt,Apply Templates,我正在使用。这是我的XML文档: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="listacd_es1.xslt"?> <listacd> <artista> <nome>Stanley Jordan</nome> <albums> <

我正在使用。这是我的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>

史坦利·乔登
魔力
1985
蓝音符
偷来的瞬间
1991
蓝音符
尼克-德雷克
粉红月亮
1972
孤岛
布吕特铺管机
1970
孤岛
还剩五片叶子
1970
孤岛
杰夫·巴克利
恩典
1994
哥伦比亚
白人男孩先生
2000
哥伦比亚
乔·塞奇尼
和外星人一起冲浪
1987
史诗
不是地球上的
1988
相对论
以下是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>

斯坦利·乔丹:
斯坦利·乔丹魔术触摸式时刻


尼克·德雷克: Nick DrakePink MoonBryter还剩下五片叶子

杰夫·巴克利: 杰夫·巴克利是一个白人男孩

乔·萨特里亚尼: 乔·萨特里亚和地球上的阿利恩诺冲浪


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

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

问题的出现是因为这个模板

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

:


特别是,行
。这将查找与当前artista元素的子节点匹配的模板,在您的示例中,这些子节点是nomealbums,它们在XSLT中都没有匹配的模板。因此,内置模板适用,对于nome元素,将输出其中的文本,这就是重复文本的来源

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

 <xsl:template match="nome" />

第二种解决方案是从artista模板中删除当前的
,而是使用一个匹配nome的模板,在那里输出值。请尝试使用此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>



:
我应该避免像“谢谢”之类的评论,但我真的必须感谢你。我现在正在学习XSLT,我讨厌它。我讨厌输出依赖于处理器,而且对我来说太冗长了。希望在这几天的考试中学好它。当你在掌握关键概念方面遇到问题时,感到沮丧是很自然的。很明显,你(和我一样)在完全理解你正在做的事情之前不喜欢编写代码,我的建议是多读一些书——最好是一本深入探讨这些概念的书(我敢说,像我的书!)。您会发现XSLT虽然不同于您在过去遇到的语言,但它非常适合它所设计的任务。您对XSLT感到沮丧的部分原因可能是因为您使用的freeformatter站点。这为Saxon HE 9.4提供了一个前端。但是,当您的代码出错时(如您所愿),它不会提供Saxon提供的所有诊断信息-它会生成诸如“无法对XML文件执行XSL转换。未能编译样式表。检测到1个错误”之类的消息,但不会告诉您错误是什么。我建议您在学习平台上使用KernowforSaxon(免费)或oXygen(提供评估版)。