Selecting and merging XML data when having the same node name -
i'm trying select data uniprot xml file, , i'm able things want out, i'm having problems getting data out have more entries in same node. make easier i'll use known cd collection data show want. know what's easiest way of saving output .csv or .txt file. thanks! let me know if unclear.
xml code:
<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited 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 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>
current xslt code:
<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited 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>
current output:
my cd collection title artist company empire burlesque bob dylan columbia abc hide heart bonnie tyler cbs records abc greatest hits dolly parton rca
so data out want, "company" results in 1 column, this: columbia;abc
also, in file i'm using, it's common there more entries in same node. however, want first 1 , not all, nodes. how can distinguish these two? when use
<xsl:for-each select="uniprot/entry"> <tr> <td> <xsl:value-of select="name"/> </td> </tr>
it returns first item. want this, think need other nodes want entries. great if me out this.
i modified xslt desired output:
updated xslt:
<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited 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' , 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' , 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 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>
for csv generation xslt may visit http://stackoverflow.com/questions/16524580/xslt-to-convert-the-nested-elements-with-comma-seperated/16534999#16534999
Comments
Post a Comment