简体   繁体   English

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

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

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. 当我得到在同一个父元素中多次使用的元素的值时,我想得到同名的每个元素而不仅仅是第一个元素。

Eg - 例如 -

<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). 现在当我通过每张CD并使用value-of输出艺术家名字时,我只得到第一个元素(有点可以理解)。 But how do I get ALL elements of the same name within a for-each loop? 但是如何在for-each循环中获取同名的所有元素? I tried doing an inner for-each loop but didn't work. 我尝试做一个内部for-each循环,但没有工作。

I'm very new to XML and how it works so please go easy on me...:-( 我是非常新的XML及其工作方式,所以请放轻松我...... :-(

<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 鲍勃迪伦2
Bob Dylan3 鲍勃迪伦3

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). 现在,当我for-each通过每个CD和利用value-of输出艺术家的名字,我只得到了第一个元素(有点理解)。 But how do I get ALL elements of the same name within a for-each loop? 但是如何在for-each循环中获取同名的所有元素? I tried doing an inner for-each loop but didn't work. 我尝试做一个内部for-each循环,但没有工作。

My advice to any XSLT novice is not to use <xsl:for-each> -- as much as possible . 我对任何XSLT新手的建议是 使用<xsl:for-each> -尽可能的 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). 我知道只有一个用例,其中<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
<xsl:template match="catalog/cd/artist">
    <xsl:value-of select="." />
</xsl:template>

There's no reason an inner loop wouldn't work. 没有理由内循环不起作用。 What XSLT syntax did you use to do this? 您使用什么XSLT语法来执行此操作? It sounds like there's an error in your xpaths or something, because what you've described should work fine. 听起来你的xpaths中有一个错误,因为你所描述的应该可以正常工作。

Your XPATH is proably wrong: 你的XPATH是错误的:

While in the CD element using a foreach: 在使用foreach的CD元素中:

  <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.. 打印所有的艺术家..

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 有些人并不特别喜欢这个网站,但是为了开始,可以指出你正确的方向 - 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基本XML / XSLT-许多节点具有相同名称且由TEXT和其他NODES组成时的值 - Basic XML/XSLT - Value-of many nodes when they are have the same name AND When they consists TEXT and other NODES XSLT 使用 concat 或多个 value-of 元素连接变量? - XSLT use concat or multiple value-of elements to join variables? XSLT价值问题,那么价值包含xml - XSLT value-of issue then value contains xml 在XSLT 1.0中,当特定XML元素是多个具有相同名称的元素之一时,如何访问它的属性? - In XSLT 1.0, how can I access attributes of a particular XML element when it is one of multiple elements with the same name? 获得XSL,XML相同名称的价值 - Get value-of in XSL, XML same names XML中多个具有相同名称的元素,并且仅希望使用xslt根据一种条件更改一个特定元素的值 - Multiple elements with same name in XML and want to change value of one specific element only based on one condition using xslt 使用value-of通过XSLT从XML元素获取值 - Getting the value from an XML element via XSLT using value-of 当多个节点/元素具有相同名称时,SQL Server在表中查询值的多个XML记录 - SQL Server, query multiple XML records in a table for a value, when multiple nodes/elements have the same name 在 XSLT 中处理多个同名元素 - Handling multiple elements with same name in XSLT 多个相同元素的XSLT变量元素名称 - XSLT for Variable Element Name for multiple same elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM