基本 XML/XSLT - 存在多个同名元素时的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8376658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:02:53  来源:igfitidea点击:

Basic XML/XSLT - value-of when there are multiple elements of the same name

xmlxsltforeachvalue-of

提问by lionysis

When I'm getting the value of an element which is used multiple times in the same parent element, I'd like to get each element of the same name and not just the first.

当我获取在同一个父元素中多次使用的元素的值时,我想获取同名的每个元素,而不仅仅是第一个。

E.g. -

例如——

<catalog>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
            <artist>Bob Dylan2</artist>
            <artist>Bob Dylan3</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</cd>
</catalog>

Now when I for-each through each CD and use value-of to output the artist name, I only get the first element (somewhat understandably). But how do I get ALL elements of the same name within a for-each loop? I tried doing an inner for-each loop but didn't work.

现在,当我 for-each 遍历每张 CD 并使用 value-of 输出艺术家姓名时,我只得到第一个元素(有点可以理解)。但是如何在 for-each 循环中获取所有同名元素?我尝试做一个内部 for-each 循环,但没有奏效。

I'm very new to XML and how it works so please go easy on me...:-(

我对 XML 及其工作原理很陌生,所以请对我放轻松...:-(

回答by Siva Charan

<xsl:template match="/">
    <xsl:for-each select="catalog">
        <!-- Print Other Stuff, if required -->
        <xsl:for-each select="cd/artist">
            <xsl:value-of select="text()"/><br/>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

Output:

输出:

Bob Dylan
Bob Dylan2
Bob Dylan3

鲍勃·迪伦
鲍勃·迪伦 2鲍勃·迪伦
3

回答by Dimitre Novatchev

Now when I for-eachthrough each CD and use value-ofto output the artist name, I only get the first element (somewhat understandably). But how do I get ALL elements of the same name within a for-eachloop? I tried doing an inner for-eachloop but didn't work.

现在,当我for-each通过每张 CD 并使用value-of输出艺术家姓名时,我只得到第一个元素(有点可以理解)。但是如何在for-each循环中获取所有同名元素?我尝试做一个内for-each循环,但没有奏效。

My advice to any XSLT novice isnotto use <xsl:for-each>-- as much as possible. I am aware of only one use-case where <xsl:for-each>is necessary, and this is a very rarely encountered case (when it is necessary to explicitly change the current document so that the key()function will use an index built for this specific document).

我对任何XSLT新手的建议是使用<xsl:for-each>-尽可能多地。我知道只有一个用例<xsl:for-each>是必要的,这是一种很少遇到的情况(当需要显式更改当前文档以便key()函数使用为该特定文档构建的索引时)。

This is probably one of the simplest possible solutions (no <xsl:for-each>and no nesting):

这可能是最简单的解决方案之一(没有<xsl:for-each>和没有嵌套):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="artist">
  <xsl:value-of select="concat(., '&#xA;')"/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

当此转换应用于提供的 XML 文档时

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <artist>Bob Dylan2</artist>
        <artist>Bob Dylan3</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
</catalog>

the wanted, correct result is produced:

产生了想要的、正确的结果

Bob Dylan
Bob Dylan2
Bob Dylan3

回答by Duong Quang Ha

<xsl:template match="catalog/cd/artist">
    <xsl:value-of select="." />
</xsl:template>

回答by nevermind

the text()xpath syntax selects any text child nodes of the current specified node.

text()XPath语法选择当前特定节点的任何文本子节点。

some don't particularly like this website, but for starting off, its not bad to point you in the right direction - click here

有些人并不特别喜欢这个网站,但对于开始来说,为您指明正确的方向也不错 -单击此处

回答by Tom W

There's no reason an inner loop wouldn't work. What XSLT syntax did you use to do this? It sounds like there's an error in your xpaths or something, because what you've described should work fine.

没有理由内循环不起作用。您使用什么 XSLT 语法来执行此操作?听起来您的 xpath 或其他内容中存在错误,因为您所描述的内容应该可以正常工作。

回答by FailedDev

Your XPATH is proably wrong:

您的 XPATH 可能是错误的:

While in the CD element using a foreach:

在 CD 元素中使用 foreach:

  <xsl:template match="/">
    <xsl:for-each select="//cd">
      <xsl:for-each select="artist">
        <xsl:message terminate="no">
          <xsl:value-of select="."/>
        </xsl:message>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

That prints all the artists..

打印所有艺术家..