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

时间:2014-07-14 13:48:31

标签: 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</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>

使用的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元素,所以我有点卡住了。关于如何做到这一点的任何建议?

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

感谢。

1 个答案:

答案 0 :(得分:2)

这是一个选项:

样式表

<?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