1

uniprot XML ファイルからいくつかのデータを選択しようとしていますが、必要なもののほとんどを取得できますが、同じノードに複数のエントリがあるデータを取得する際に問題があります。簡単にするために、よく知られている CD コレクション データを使用して、必要なものを示します。出力を .csv または .txt ファイルとして保存する最も簡単な方法も知りたいです。ありがとう!不明な点があればお知らせください。

XML コード:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <company>ABC</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <company>ABC</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>

現在の XSLT コード:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
        <th>Company</th>
      </tr>
    <xsl:apply-templates />
    </table>
  </body>
  </html>
</xsl:template>

    <xsl:template match="catalog/cd">
      <tr>
        <xsl:apply-templates select="title|artist|company"/>
      </tr>
    </xsl:template>


    <xsl:template match="title|artist|company">
      <td>
        <xsl:value-of select="."/>
      </td>
    </xsl:template>

</xsl:stylesheet>

現在の出力:

My CD Collection

Title           Artist          Company
Empire Burlesque    Bob Dylan   Columbia    ABC
Hide your heart Bonnie Tyler    CBS Records ABC
Greatest Hits   Dolly Parton    RCA

したがって、必要なすべてのデータを取得しますが、「会社」の結果を次のように 1 列に表示したいと考えています。Columbia;ABC

また、私が使用しているファイルでは、同じノードに複数のエントリがあるのが一般的です。ただし、ほとんどの場合、すべてではなく最初のノードが必要であり、一部のノードのみが必要です。この2つをどうやって区別できますか?私が使うとき

<xsl:for-each select="uniprot/entry"> 
  <tr>
    <td>
      <xsl:value-of select="name"/>
    </td>
  </tr>

最初のアイテムを返すだけです。ほとんどの場合、これが必要ですが、すべてのエントリが必要な他のノードが必要だと思います。これで私を助けていただければ幸いです。

4

1 に答える 1

1

目的の出力が得られるように xslt を少し変更しました。

更新された xslt:

   <?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <h2>My CD Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Artist</th>
            <th>Company</th>
          </tr>
          <xsl:apply-templates/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="catalog/cd">
    <tr>
      <xsl:apply-templates select="title|artist|company"/>
    </tr>
  </xsl:template>


  <xsl:template match="title|artist|company">
    <xsl:choose>
      <xsl:when test="name()='company' and not(preceding-sibling::company)">
        <td>
          <xsl:value-of select="."/>
          <xsl:if test="following-sibling::company">
            <xsl:text>;</xsl:text>
            <xsl:for-each select="following-sibling::company">
              <xsl:value-of select="."/>
              <xsl:if test="position()!=last()">
                <xsl:text>;</xsl:text>
              </xsl:if>
            </xsl:for-each>
          </xsl:if>
        </td>
      </xsl:when>
      <xsl:when test="name()='company' and preceding-sibling::company"/>
      <xsl:otherwise>
        <td>
          <xsl:value-of select="."/>
        </td>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

出力:

  <html>
   <body>
      <h2>My CD Collection</h2>
      <table border="1">
         <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Artist</th>
            <th>Company</th>
         </tr>

         <tr>
            <td>Empire Burlesque</td>
            <td>Bob Dylan</td>
            <td>Columbia;ABC</td>
         </tr>

         <tr>
            <td>Hide your heart</td>
            <td>Bonnie Tyler</td>
            <td>CBS Records;ABC</td>
         </tr>

         <tr>
            <td>Greatest Hits</td>
            <td>Dolly Parton</td>
            <td>RCA</td>
         </tr>

      </table>
   </body>
</html>

CSV 生成 XSLT については、次の URL にアクセスしてください。http://stackoverflow.com/questions/16524580/xslt-to-convert-the-nested-elements-with-comma-seperated/16534999#16534999

于 2013-05-14T06:31:38.277 に答える