0

I have an XML like this

<A Rank="1">
  <a1>1</a2>
  <a1>1</a2>
</A1>
<A Rank="2">
  <a1>1</a2>
  <a1>1</a2>
</A1>

My XSLT sample is for using that Rank value.

<tr>
  <td align="left">
    <xsl:text>#</xsl:text>
    <xsl:value-of select="@Rank"/>
    <xsl:text>:</xsl:text>
    <xsl:value-of select="a1"/>
  </td>
</tr>

I want to print the rank value. How can I?

4

1 回答 1

0

您正确的 XSLT 应如下所示:

<t>
  <A Rank="1">
    <a1>1</a1>
    <a2>1</a2>
  </A>
  <A Rank="2">
    <a1>1</a1>
    <a2>4</a2>
  </A>
</t>

XSLT1:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="A">
    <tr>
      <td align="left">
        <xsl:text>#</xsl:text>
        <xsl:value-of select="@Rank"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="sum(child::*)"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

输出1:

  <tr><td align="left">#1:2</td></tr>
  <tr><td align="left">#2:5</td></tr>

xslt2:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="A">
    <tr>
      <td align="left">
        <xsl:text>#</xsl:text>
        <xsl:value-of select="@Rank"/>
        <xsl:text>:</xsl:text>
        <xsl:for-each select="child::*">
          <xsl:value-of select="."/>
          <xsl:if test="position()!=last()">
            <xsl:text>:</xsl:text>
          </xsl:if>
        </xsl:for-each>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

输出2:

  <tr><td align="left">#1:1:1</td></tr>
  <tr><td align="left">#2:1:4</td></tr>
于 2013-05-29T04:49:31.377 回答