2013-05-14 98 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

相关问题