XSLT:按XML中的childNode排序

时间:2018-04-07 13:42:30

标签: xml xslt xpath

我在根节点管理器中有多个名为group的节点。我需要输出已排名的歌曲以及所有组节点的相应专辑和组名,然后按元素rank_song排序。

   <?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>

下一个xslt文件打印所有具有rank_song元素的歌曲,但我不知道如何用数字排序:

<?xml version="1.0" encoding="UTF-8"?>
<!-- DWXMLSource="alumnos.xml" -->
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

    <xsl:template match="/">
<HTML>
  <HEAD>
    <TITLE>Example</TITLE>
  </HEAD>
  <BODY>

    <xsl:for-each select="soundvisor/groups/group">


            <h1>Name of grup: <xsl:value-of select='@name_g' /></h1> 
            <xsl:variable name="group">
              <xsl:value-of select='@name_g' />
             </xsl:variable>

              <xsl:for-each select="album">

                <xsl:if test="count(song/rank_song)">

                  <h2>Name album: <xsl:value-of select='@name' /></h2>
                  <xsl:variable name="album">
                      <xsl:value-of select='@name' />
                  </xsl:variable>



                  <xsl:for-each select="song">

                      <xsl:if test="count(rank_song)">



                        <h3>Name song: <xsl:value-of select='@name' /></h3> 
                        <xsl:variable name="song">
                          <xsl:value-of select='@name' />
                        </xsl:variable>

                         <h4>Rank song: <xsl:value-of select='rank_song' /></h4> 
                        <xsl:variable name="rank">
                          <xsl:value-of select='rank_song' />
                        </xsl:variable>
                      </xsl:if>


                  </xsl:for-each>
                  </xsl:if>

            </xsl:for-each>
    </xsl:for-each> 
  </BODY>
</HTML>
</xsl:template>

</xsl:stylesheet>

image of output received from xslt

second output using match

output expected from xslt

1 个答案:

答案 0 :(得分:1)

要对输出进行排序,只需在XSLT文件中的<xsl:sort>元素内添加<xsl:for-each>元素:

<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>