2

私は次のxmlを持っています:

<?xml version="1.0" encoding="ISO-8859-1"?>

<Loci>
    <Locus>
        <Id>MTBC1726</Id>
        <Alleles>
            <Allele>
                <Name>1.0</Name>
                <Description>No annotation provided</Description>
            </Allele>
            <Allele>
                <Name>2.0</Name>
                <Description>No annotation provided</Description>
            </Allele>
        </Alleles>
    </Locus>
    <Locus>
        <Id>MTBC3142</Id>
        <Alleles>
            <Allele>
                <Name>1.0</Name>
                <Description>No annotation provided</Description>
            </Allele>
        </Alleles>
    </Locus>
</Loci>

そして、次の結果を作成したいと思います。

ここに画像の説明を入力

HTML では、次のようになります。

<html>
<body>

<table border="1">
  <tr>
    <th>Locus</th>
    <th>Allele</th>
    <th>Description</th>
  </tr>
  <tr>
    <td rowspan="2">MTBC1726</td>
    <td>1.0</td>
    <td >No annotation provided</td>
  </tr>
  <tr>
    <td>2.0</td>
    <td >No annotation provided</td>
  </tr>
  <tr>
    <td >MTBC3142</td>
    <td>1.0</td>
    <td >No annotation provided</td>
  </tr>
</table>

</body>
</html>

次の XSL を作成しました。

<?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="/">
  <html>
  <body>
    <table border="1">
      <tr>
        <th>Locus</th>
        <th>Allele</th>
        <th>Description</th>
      </tr>
      <xsl:for-each select="Loci/Locus">
      <tr>
        <td><xsl:value-of select="Id"/></td>
        <xsl:for-each select="Alleles/Allele">
          <td><xsl:value-of select="Name"/></td>
          <td><xsl:value-of select="Description"/></td>
        </xsl:for-each>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

しかし、それは次のようなテーブルを生成します:

ここに画像の説明を入力

だから、私の質問は次のとおりです。ノードrowspanの数に基づいたカウントで属性を追加するにはどうすればよいですか?<Allele>

4

3 に答える 3

4

これはどう:

<table border="1">
  <tr>
    <th>Locus</th>
    <th>Allele</th>
    <th>Description</th>
  </tr>
  <xsl:for-each select="Loci/Locus">
    <xsl:for-each select="Alleles/Allele">
      <tr>
        <xsl:if test="position() = 1">
          <td rowspan="{last()}">
            <xsl:value-of select="ancestor::Locus[1]/Id"/>
          </td>
        </xsl:if>
        <td><xsl:value-of select="Name"/></td>
        <td><xsl:value-of select="Description"/></td>
      </tr>
    </xsl:for-each>
  </xsl:for-each>
</table>

ここでの考え方は、インナー内でfor-each使用して、thisposition()の最初にいるかどうかを確認し、現在の要素に含まれる要素の数を与えることができるということです。これは、現在のコンテキストが最初の時点で Idを抽出する必要があるためです。AlleleLocuslast()AlleleLocusancestor::Locus[1]LocusAllele

rowspan="1"単一対立遺伝子座の追加を避けたい場合xsl:attributeは、属性値テンプレートの代わりに条件を使用する必要があります。

        <xsl:if test="position() = 1">
          <td>
            <xsl:if test="last() &gt; 1">
              <xsl:attribute name="rowspan">
                <xsl:value-of select="last()" />
              </xsl:attribute>
            </xsl:if>
            <xsl:value-of select="ancestor::Locus[1]/Id"/>
          </td>
        </xsl:if>
于 2013-10-30T13:59:32.107 に答える