XSLT:在XML中按子节点排序
我在根节点soundvisor中有一个以上名为group的节点。我只需要输出所有组节点的已排名歌曲及其各自的唱片集和组名,然后按元素rank_song排序XSLT:在XML中按子节点排序,xml,xslt,xpath,Xml,Xslt,Xpath,我在根节点soundvisor中有一个以上名为group的节点。我只需要输出所有组节点的已排名歌曲及其各自的唱片集和组名,然后按元素rank_song排序 <?xml version='1.0' encoding='UTF-8' standalone="yes"?> <!DOCTYPE soundvisor SYSTEM "soundvisor.dtd"> <soundvisor> <groups> <group name_
<?xml version='1.0' encoding='UTF-8' standalone="yes"?>
<!DOCTYPE soundvisor SYSTEM "soundvisor.dtd">
<soundvisor>
<groups>
<group name_g='ACDC'>
<city>Sidney</city>
<country>Australia</country>
<year_born>1973</year_born>
<type_music>Hard Rock</type_music>
<type_music>Blues</type_music>
<img>path_to_img</img>
<link>path_link</link>
<artist alias='Brian_Johnson'>
<name>Brian Francis Johnson De Luca</name>
<date_born>05/10/1947</date_born>
<city_artist>Dunston</city_artist>
<country_artist>Unted Kingdom</country_artist>
<instrument>Vocal</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<artist alias='Angus_Young'>
<name>Angus McKinnon Young</name>
<date_born>31/03/1955</date_born>
<city_artist>Glasgow</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Guitar</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<artist alias='Malcolm_Young'>
<name>Malcolm Mitchell Young</name>
<date_born>06/01/1953</date_born>
<city_artist>Glasgow</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Guitar</instrument>
<instrument>Vocal</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<artist alias='Cliff_Williams'>
<name>Clifford Williams</name>
<date_born>14/12/1949</date_born>
<city_artist>Romford</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Bass guitar</instrument>
<instrument>Vocal</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<album name='Highway_to_Hell'>
<length_album>41:34</length_album>
<year>1979</year>
<img_album>path_to_img</img_album>
<song name='Highway_to_Sky'>
<length>3:28</length>
</song>
<song name='Girls_Got_Rhythm'>
<length>3:24</length>
<rank_song>4</rank_song>
</song>
<song name='Walk_All_Over_You'>
<length>5:09</length>
</song>
</album>
<album name='Black_in_Black'>
<length_album>41:59</length_album>
<year>1980</year>
<img_album>path_to_img</img_album>
<song name='Hells_Bells'>
<length>5:13</length>
</song>
<song name='Shoot_to_Thrill'>
<length>5:20</length>
<rank_song>5</rank_song>
</song>
<song name='What_Do_You_Do_for_Money_Honey'>
<length>3:36</length>
</song>
</album>
</group>
<group name_g='The_Beatles'>
<city>Liverpool</city>
<country>Spain</country>
<year_born>1964</year_born>
<type_music>Pop</type_music>
<type_music>Rock</type_music>
<img>path_to_img</img>
<link>path_link</link>
<artist alias='John_Lenon'>
<name>John Winston Ono Lennon</name>
<date_born>09/10/1940</date_born>
<city_artist>Liverpool</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Guitar</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<artist alias='Paul_McCartney'>
<name>James Paul McCartney</name>
<date_born>18/06/1942</date_born>
<city_artist>Liverpool</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Guitar</instrument>
<instrument>Vocal</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<artist alias='George_Harrison'>
<name>George Harrison</name>
<date_born>25/02/1943</date_born>
<city_artist>Liverpool</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Guitar</instrument>
<instrument>Vocal</instrument>
<instrument>Piano</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<artist alias='Ringo_Starr'>
<name>Richard Henry Starkey</name>
<date_born>07/07/1940</date_born>
<city_artist>Liverpool</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Drums</instrument>
<instrument>Vocal</instrument>
<instrument>Guitar</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<album name='Help'>
<length_album>30:45</length_album>
<year>1965</year>
<img_album>path_to_img</img_album>
<song name='Sister'>
<length>4:02</length>
</song>
<song name='Rubber_Soul'>
<length>3:24</length>
<rank_song>2</rank_song>
</song>
<song name='Beatles_for_Sale'>
<length>5:09</length>
</song>
</album>
<album name='Beatles'>
<length_album>93:35</length_album>
<year>1968</year>
<img_album>path_to_img</img_album>
<song name='Yellow_Submarine'>
<length>5:13</length>
</song>
</album>
<album name='Let_It_Be'>
<length_album>30:323</length_album>
<year>1968</year>
<img_album>path_to_img</img_album>
<song name='Dig_a_Pony'>
<length>3:54</length>
</song>
</album>
</group>
<group name_g='Supertramp'>
<city>London</city>
<country>United Kingdom</country>
<year_born>1980</year_born>
<type_music>Rock</type_music>
<type_music>Pop</type_music>
<img>path_to_img</img>
<link>path_link</link>
<artist alias='Roger_Hodson'>
<name>Charles Roger Pomfret Hodgson</name>
<date_born>21/03/1950</date_born>
<city_artist>Portsmouth</city_artist>
<country_artist>Unted Kingdom</country_artist>
<instrument>Vocal</instrument>
<instrument>Piano</instrument>
<instrument>Guitar</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<artist alias='Frank_Farrell'>
<name>Frank Farrell Magnuson</name>
<date_born>31/03/1947</date_born>
<city_artist>Birmingham</city_artist>
<country_artist>United Kingdom</country_artist>
<instrument>Guitar</instrument>
<img_artist>path_to_img</img_artist>
</artist>
<album name='Breakfast_in_America'>
<length_album>41:34</length_album>
<year>1979</year>
<img_album>path_to_img</img_album>
<song name='Gone_Hollywood'>
<length>4:28</length>
</song>
<song name='The_Logical_Song'>
<length>3:24</length>
<rank_song>1</rank_song>
</song>
<song name='Goodbye_Stranger'>
<length>5:09</length>
<rank_song>3</rank_song>
</song>
</album>
</group>
</groups>
</soundvisor>
西德尼
澳大利亚
1973
硬岩
忧郁
路径_至_img
路径链接
要对输出进行排序,只需在XSLT文件的
元素中添加
元素:
<xsl:for-each select="soundvisor/groups/group/album/song">
<xsl:sort select="rank_song" data-type="number" />
<xsl:if test="count(rank_song)">
<h1>Name of group:
<xsl:value-of select='../../@name_g' />
</h1>
<h2>Name album:
<xsl:value-of select='../@name' />
</h2>
<h3>Name song:
<xsl:value-of select='@name' />
</h3>
<h4>Rank song:
<xsl:value-of select='rank_song' />
</h4>
</xsl:if>
</xsl:for-each>
小组名称:
相册名称:
歌曲名称:
排名歌曲:
您提供的XML示例似乎不完整。是的,我刚刚再次上传了它!我认为它不能被正确排序,因为XMLIt的结构,如果您显示您期望的输出(最好不是文本,而是在实际问题中),它也会有所帮助。谢谢还要注意,match
在xsl:sort
上无效。因此,如果您的XLST实际上没有抛出错误,那么很可能只是忽略排序。感谢Tim C的关注,我刚刚添加了一张有关XLST文件预期输出问题的照片。嗯,我正在使用排序标记上的匹配,没有看到任何错误。非常感谢!我的解决方案是简化另一个xml。你向我提出的解决方案很好,令人惊讶,非常感谢!
<xsl:for-each select="soundvisor/groups/group/album/song">
<xsl:sort select="rank_song" data-type="number" />
<xsl:if test="count(rank_song)">
<h1>Name of group:
<xsl:value-of select='../../@name_g' />
</h1>
<h2>Name album:
<xsl:value-of select='../@name' />
</h2>
<h3>Name song:
<xsl:value-of select='@name' />
</h3>
<h4>Rank song:
<xsl:value-of select='rank_song' />
</h4>
</xsl:if>
</xsl:for-each>