I want to create XML file from XML using XSLT. At the beginning i want to display all the elements called "track", i want to display it in XML.
Here is my XML file
<document> <recordlist> <bestseller year="2015"> <plate id="p01" kind="Pop&Rock"> <title>Atramentowa</title> <performer>Celińska Stanisława</performer> <release_date>2015-05-08</release_date> <distributor idref="d1" /> <price currency="PLN">37,49</price> <year_recorded>2015</year_recorded> <availability mp3="1" cd="0" dvd="1" /> <playlist id="pl01" nazwa="Atramentowa"> <track id="p01u01">Czerń i biel<czas>3:27</czas></track> <track id="p01u02">Czy o kimś ktoś<czas>2:17</czas></track> <track id="p01u03">Atramentowa rumba<czas>5:16</czas></track> <track id="p01u04">Do rycerzy, do szlachty, do mieszczan<czas>3:22</czas></track> <track id="p01u05">Drzwi odemknij<czas>3:21</czas></track> <track id="p01u06">Moje życie, twoje życie<czas>3:19</czas></track> <track id="p01u07">Obfitość<czas>3:20</czas></track> <track id="p01u08">Jego portret<czas>3:15</czas></track> <track id="p01u09">I znowu on<czas>3:11</czas></track> <track id="p01u10">Nie strasz<czas>3:55</czas></track> <track id="p01u11">Smuteczku mój<czas>3:44</czas></track> <track id="p01u12">Szeptem do mnie mów<czas>3:33</czas></track> <track id="p01u13">Wielka słota<czas>3:11</czas></track> <track id="p01u14">Wakacje z deszczem<czas>2:59</czas></track> <track id="p01u15">Pieśń cygańska<czas>3:09</czas></track> </playlist> </plate> </bestseller> </recordlist> </document> I tried to display all the elements "track" in each "playlist", but always i had wrong effect, like this:
<?xml version="1.0" encoding="UTF-8"?> <document> <recordlist> <bestseller> <year>2015</year> <plate> <kind>Pop&Rock</kind> <title>Atramentowa</title> <performer>Celińska Stanisława</performer> <release_date>2015-05-08</release_date> <price>37,49</price> <year_recorded>2015</year_recorded> <playlist> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> <track /> </playlist> </plate> </bestseller> </recordlist> </document> I want to see the name of each track.
Here it is what I've done so far, in xslt:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date" xmlns:math="http://www.exslt.org/math"> <xsl:output method="xml" version="1.0" encoding="UTF-8" media- type="text/xml" omit-xml-declaration="no" indent="yes" /> <xsl:template match="*"> <xsl:apply-templates select="@TEXT | node()"/> </xsl:template> <xsl:template match="node/@TEXT | text()"> <xsl:if test="normalize-space(.)"> <xsl:value-of select="concat(normalize-space(.), ' ')"/> </xsl:if> <xsl:apply-templates /> </xsl:template> <xsl:template match="//recordlist"> <xsl:element name="recordlist"> <xsl:apply-templates select="bestseller"/> </xsl:element> </xsl:template> <xsl:template match="//bestseller"> <xsl:element name="bestseller"> <xsl:element name="year"> <xsl:value-of select="./@year"/> </xsl:element> <xsl:apply-templates select="plate"/> </xsl:element> </xsl:template> <xsl:template match="//plate"> <xsl:element name="plate"> <xsl:element name="kind"> <xsl:value-of select="./@kind"/> </xsl:element> <xsl:element name="title"> <xsl:value-of select="./title"/> </xsl:element> <xsl:element name="performer"> <xsl:value-of select="./performer"/> </xsl:element> <xsl:element name="release_date"> <xsl:value-of select="./release_date"/> </xsl:element> <xsl:element name="price"> <xsl:value-of select="./price"/> </xsl:element> <xsl:element name="year_recorded"> <xsl:value-of select="./year_recorded"/> </xsl:element> <xsl:apply-templates select="playlist"/> </xsl:element> </xsl:template> <xsl:template match="//playlist"> <xsl:element name="playlist"> <xsl:for-each select="track"> <xsl:element name="track"> <xsl:value-of select="./track"/> </xsl:element> </xsl:for-each> </xsl:element> </xsl:template> </xsl:stylesheet> If anyone know how to do it, i would be very grateful.
No comments:
Post a Comment