2013-05-14 101 views
1

我試圖從uniprot XML文件中選擇一些數據,並且我能夠獲得我想要的大部分內容,但是我在獲取數據時遇到了問題在同一個節點中有更多的條目。爲了使它更容易,我將使用衆所周知的CD收集數據來顯示我想要的。我還想知道將輸出保存爲.csv或.txt文件的最簡單方法是什麼。謝謝!如果有什麼不清楚,請告訴我。具有相同節點名稱時選擇併合並XML數據

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

另外,在我使用的文件中,通常在同一個節點中有更多的條目。但是,對於大多數我只是想要第一個而不是全部,只有一些節點。你怎麼能區分這兩個?當我使用

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

它只是返回第一項。所以我主要想這個,但我想我需要其他節點,我想要所有條目。如果你能幫助我解決這個問題,那將會很棒。

回答

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> 

OUTPUT:

<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您可以訪問http://stackoverflow.com/questions/16524580/xslt-to-convert-the-nested-elements-with-comma-seperated/16534999#16534999

+0

謝謝!這有很大幫助。唯一的問題是,公司的第二個條目還有一個額外的列。你怎麼能確保這不會發生? (在原始文件中,這意味着很多額外的列使得處理更加困難) – user1941884 2013-05-14 06:57:40

+0

附加問題(不必要,但可能使事情更美麗)。設想2個節點有幾個條目,但條目數量是相同的。第一個節點包含例如:第二個節點包含: 一二三四五 ,而不是領輸出: 1; 2; 3; 4; 5;二則,三,四,五 有可能得到這個: 1 - 1; 2 - 2; 3 - 3; 4 - 4; 5 - 5 – user1941884 2013-05-14 06:58:25

+0

我已經更新了我的回覆,解決您的第一個查詢 - 額外的列。你的第二個問題也是可以接受的,但你需要分享輸入XML和期望的輸出。 – 2013-05-14 07:10:30

相關問題