Xml 使用XSLT显示具有相同名称的多个属性

Xml 使用XSLT显示具有相同名称的多个属性,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我对xml和xslt的世界相当陌生。我想做的是使用xslt返回xml文件中生成的所有错误消息,每个父项下都有许多错误消息 下面是XML文件的一个示例: <progress_file> <read_leg> <info>Successfully read face ID 225</info> <info>successfully read face ID 226<

我对xml和xslt的世界相当陌生。我想做的是使用xslt返回xml文件中生成的所有错误消息,每个父项下都有许多错误消息

下面是XML文件的一个示例:

   <progress_file>
        <read_leg>
            <info>Successfully read face ID 225</info>
            <info>successfully read face ID 226</info>
            <error>unable to read face ID 227</error>
            <error>unable to read face ID 228</error>
        </read_leg>
        <write_leg>
            <info>Successfully created face ID 225</info>
            <info>successfully created face ID 226</info>
            <error>unable to write face ID 227</error>
            <error>unable to write face ID 228</error>
        </write_leg>
    </progress_file>

已成功读取面ID 225
已成功读取面ID 226
无法读取面ID 227
无法读取面ID 228
已成功创建面ID 225
已成功创建面ID 226
无法写入面ID 227
无法写入面ID 228
使用的XSLT是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
      <xsl:for-each select="progress_file/read_leg">
        <xsl:value-of select="error"/>
      </xsl:for-each>
      <xsl:for-each select="progress_file/write_leg">
        <xsl:value-of select="error"/>
      </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

输出仅返回每个区域的第一个值。我推测这就是逻辑所暗示的,即“对于每个写段,返回错误消息”,这并不意味着它会检查是否存在多个情况

我还没有看到任何地方有多个同名属性,也没有遇到可以处理这些属性的XSL元素,所以我有点卡住了。关于如何做到这一点,有什么建议吗

还有一个问题,是否可能在输出行之间获得换行符

谢谢。

这里有一个选项:

样式表
你可以将你的匹配缩短为
progress\u file/*/error
,假设他希望处理
progress\u file
下的所有潜在错误元素。“先读后写”是一种误解。选择节点的顺序无关紧要,结果节点集在XSLT中总是按文档顺序。(顺便说一句,XSLT 2.0也是如此:单个XPath表达式将生成文档顺序的序列。但是,您也可以构建自定义顺序序列。)@MatthewGreen仅仅因为您可以,并不意味着您应该这样做。@Tomalak:您完全正确,感谢您指出这一点。我想到的是
而不是
|
。谢谢,这会输出正确的数据。但是换行选项不起作用。我已经在IE中打开了XML,并尝试使用W3学校网站上的TryIt编辑器,所有信息都显示在一行上。你知道为什么会这样吗?@EeroHelenius请不要编辑OP的代码;你不知道原文是否正确。@michael.hor257k:我很抱歉,谢谢你指出。我只是假设代码中有一个输入错误,因为它与示例输入不一致。但你是对的,做出这样的假设可能不安全——我将来会更清楚的。
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" version="1.0" encoding="utf-8"/>

  <!-- The XML entity for a line feed -->
  <xsl:variable name="linefeed" select="'&#10;'"/>

  <!-- Match the root node -->
  <xsl:template match="/">
    <!-- Apply templates for all <error> nodes. -->
    <xsl:apply-templates select="progress_file/read_leg/error | progress_file/write_leg/error"/>
  </xsl:template>

  <xsl:template match="error">
    <!-- Concatenate the value of the current node and a line feed. -->
    <xsl:value-of select="concat(., $linefeed)"/>
  </xsl:template>
</xsl:stylesheet>
unable to read face ID 227
unable to read face ID 228
unable to write face ID 227
unable to write face ID 228