在XSL中添加子元素进行转换

时间:2019-12-19 06:54:12

标签: xml xslt

我正在尝试从XML下面创建一个XSL

<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE countries SYSTEM "countries.dtd"> -->
<!-- xmlns="http://www.example.org/countries" -->
<countries>
  <!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:noNamespaceSchemaLocation="countries.xsd" -->
  <country name="Afghanistan" population="22664136" area="647500">
    <language percentage="11">Turkic</language>
    <language percentage="35">Pashtu</language>
    <language percentage="50">Afghan Persian</language>
  </country>
  <country name="Albania" population="3249136" area="28750" />
  <country name="Algeria" population="29183032" area="2381740">
    <city>
      <name>Algiers</name>
      <population>1507241</population>
    </city>
  </country>
  <country name="American Samoa" population="59566" area="199" />
  <country name="Andorra" population="72766" area="450" />
  <country name="Angola" population="10342899" area="1246700" />
  <country name="Anguilla" population="10424" area="91">
    <language percentage="100">English</language>
  </country>
  <country name="Antigua and Barbuda" population="65647" area="440">
    <language percentage="100">English</language>
  </country>
  <country name="Argentina" population="34672996" area="2766890">
    <city>
      <name>La Matanza</name>
      <population>1111811</population>
    </city>
    <city>
      <name>Cordoba</name>
      <population>1208713</population>
    </city>
    <city>
      <name>Rosario</name>
      <population>1118984</population>
    </city>
    <city>
      <name>Buenos Aires</name>
      <population>2988006</population>
    </city>
  </country>
  <country name="Armenia" population="3463574" area="29800">
    <city>
      <name>Yerevan</name>
      <population>1200000</population>
    </city>
    <language percentage="2">Russian</language>
    <language percentage="96">Armenian</language>
  </country>
  <country name="Aruba" population="67794" area="193" />
  <country name="Australia" population="18260864" area="7686850">
    <city>
      <name>Sydney</name>
      <population>3657000</population>
    </city>
    <city>
      <name>Brisbane</name>
      <population>1302000</population>
    </city>
    <city>
      <name>Adelaide</name>
      <population>1050000</population>
    </city>
    <city>
      <name>Melbourne</name>
      <population>3081000</population>
    </city>
    <city>
      <name>Perth</name>
      <population>1193000</population>
    </city>
    <language percentage="100">English</language>
  </country>
  <country name="Austria" population="8023244" area="83850">
    <city>
      <name>Vienna</name>
      <population>1583000</population>
    </city>
    <language percentage="100">German</language>
  </country>
  <country name="Azerbaijan" population="7676953" area="86600">
    <city>
      <name>Baku</name>
      <population>1740000</population>
    </city>
    <language percentage="3">Russian</language>
    <language percentage="2">Armenian</language>
    <language percentage="89">Azeri</language>
  </country>
  <country name="Bahamas" population="259367" area="13940" />
  <country name="Bahrain" population="590042" area="620" />
  <country name="Bangladesh" population="123062800" area="144000">
    <city>
      <name>Dhaka</name>
      <population>3839000</population>
    </city>
    <city>
      <name>Chittagong</name>
      <population>1599000</population>
    </city>
  </country>
  <country name="Barbados" population="257030" area="430">
    <language percentage="100">English</language>
  </country>
  <country name="Belarus" population="10415973" area="207600">
    <city>
      <name>Minsk</name>
      <population>1540000</population>
    </city>
  </country>
  <country name="Belgium" population="10170241" area="30510">
    <language percentage="32">French</language>
    <language percentage="1">German</language>
    <language percentage="56">Dutch</language>
  </country>
  <country name="Belize" population="219296" area="22960" />
  <country name="Benin" population="5709529" area="112620" />
  <country name="Bermuda" population="62099" area="50">
    <language percentage="100">English</language>
  </country>
  <country name="Bhutan" population="1822625" area="47000" />
  <country name="Bolivia" population="7165257" area="1098580" />
  <country name="Bosnia and Herzegovina" population="2656240"
    area="51233">
    <language percentage="99">Serbo-Croatian</language>
  </country>
  <country name="Botswana" population="1477630" area="600370" />
  <country name="Brazil" population="162661216" area="8511965">
    <city>
      <name>Manaus</name>
      <population>1158265</population>
    </city>
    <city>
      <name>Salvador</name>
      <population>2209465</population>
    </city>
    <city>
      <name>Fortaleza</name>
      <population>1967365</population>
    </city>
    <city>
      <name>Belo Horizonte</name>
      <population>2091770</population>
    </city>
    <city>
      <name>Belem</name>
      <population>1142258</population>
    </city>
    <city>
      <name>Curitiba</name>
      <population>1465698</population>
    </city>
    <city>
      <name>Recife</name>
      <population>1342877</population>
    </city>
    <city>
      <name>Rio de Janeiro</name>
      <population>5533011</population>
    </city>
    <city>
      <name>Porto Alegre</name>
      <population>1286251</population>
    </city>
    <city>
      <name>Sao Paulo</name>
      <population>9811776</population>
    </city>
    <city>
      <name>Brasilia</name>
      <population>1817001</population>
    </city>
  </country>
  <country name="British Virgin Islands" population="13195"
    area="150">
    <language percentage="100">English</language>
  </country>
  <country name="Brunei" population="299939" area="5770" />
  <country name="Bulgaria" population="8612757" area="110910">
    <city>
      <name>Sofia</name>
      <population>1300000</population>
    </city>
    <language percentage="100">Bulgarian</language>
  </country>
  <country name="Burkina Faso" population="10623323" area="274200" />
  <country name="Burma" population="45975624" area="678500">
    <city>
      <name>Rangoon</name>
      <population>2513000</population>
    </city>
    <language percentage="100">Burmese</language>
  </country>
  <country name="Burundi" population="5943057" area="27830" />
  <country name="Cambodia" population="10861218" area="181040" />
  <country name="Cameroon" population="14261557" area="475440" />
  <country name="Canada" population="28820672" area="9976140">
    <city>
      <name>Montreal</name>
      <population>1017666</population>
    </city>
  </country>
  <country name="Cape Verde" population="449066" area="4030" />
  <country name="Cayman Islands" population="34646" area="260">
    <language percentage="100">English</language>
  </country>
  <country name="Central African Republic" population="3274426"
    area="622980" />
  <country name="Chad" population="6976845" area="1284000" />
  <country name="Chile" population="14333258" area="756950">
    <city>
      <name>Santiago</name>
      <population>4318000</population>
    </city>
    <language percentage="100">Spanish</language>
  </country>
  <country name="China" population="1210004992" area="9596960">
    <city>
      <name>Hong Kong</name>
      <population>6218000</population>
    </city>
    <city>
      <name>Hefei</name>
      <population>1000000</population>
    </city>
    <city>
      <name>Huainan</name>
      <population>1200000</population>
    </city>
    <city>
      <name>Lanzhou</name>
      <population>1510000</population>
    </city>
    <city>
      <name>Guangzhou</name>
      <population>3580000</population>
    </city>
    <city>
      <name>Guiyang</name>
      <population>1530000</population>
    </city>
    <city>
      <name>Shijiazhuang</name>
      <population>1320000</population>
    </city>
    <city>
      <name>Tangshan</name>
      <population>1500000</population>
    </city>
    <city>
      <name>Handan</name>
      <population>1110000</population>
    </city>
    <city>
      <name>Harbin</name>
      <population>2830000</population>
    </city>
    <city>
      <name>Qiqihar</name>
      <population>1380000</population>
    </city>
    <city>
      <name>Zhengzhou</name>
      <population>1710000</population>
    </city>
    <city>
      <name>Luoyang</name>
      <population>1190000</population>
    </city>
    <city>
      <name>Wuhan</name>
      <population>3750000</population>
    </city>
    <city>
      <name>Changsha</name>
      <population>1330000</population>
    </city>
    <city>
      <name>Nanjing</name>
      <population>2500000</population>
    </city>
    <city>
      <name>Fuzhou</name>
      <population>1290000</population>
    </city>
    <city>
      <name>Nanchang</name>
      <population>1350000</population>
    </city>
    <city>
      <name>Jilin</name>
      <population>1270000</population>
    </city>
    <city>
      <name>Changchun</name>
      <population>2110000</population>
    </city>
    <city>
      <name>Shenyang</name>
      <population>4540000</population>
    </city>
    <city>
      <name>Dalian</name>
      <population>2400000</population>
    </city>
    <city>
      <name>Anshan</name>
      <population>1390000</population>
    </city>
    <city>
      <name>Fushun</name>
      <population>1350000</population>
    </city>
    <city>
      <name>Xian</name>
      <population>2760000</population>
    </city>
    <city>
      <name>Jinan</name>
      <population>2320000</population>
    </city>
    <city>
      <name>Zibo</name>
      <population>2460000</population>
    </city>
    <city>
      <name>Qingdao</name>
      <population>2060000</population>
    </city>
    <city>
      <name>Taiyuan</name>
      <population>1960000</population>
    </city>
    <city>
      <name>Datong</name>
      <population>1110000</population>
    </city>
    <city>
      <name>Chengdu</name>
      <population>2810000</population>
    </city>
    <city>
      <name>Chongqing</name>
      <population>2980000</population>
    </city>
    <city>
      <name>Kunming</name>
      <population>1520000</population>
    </city>
    <city>
      <name>Hangzhou</name>
      <population>1340000</population>
    </city>
    <city>
      <name>Ningbo</name>
      <population>1090000</population>
    </city>
    <city>
      <name>Nanning</name>
      <population>1070000</population>
    </city>
    <city>
      <name>Baotou</name>
      <population>1200000</population>
    </city>
    <city>
      <name>Urumqi</name>
      <population>1160000</population>
    </city>
    <city>
      <name>Beijing</name>
      <population>7000000</population>
    </city>
    <city>
      <name>Shanghai</name>
      <population>7830000</population>
    </city>
    <city>
      <name>Tianjin</name>
      <population>5770000</population>
    </city>
  </country>
  <country name="Christmas Island" population="813" area="135">
    <language percentage="100">English</language>
  </country>
  <country name="Cocos Islands" population="609" area="14">
    <language percentage="100">English</language>
  </country>
  <country name="Colombia" population="36813160" area="1138910">
    <city>
      <name>Medellin</name>
      <population>1621356</population>
    </city>
    <city>
      <name>Barranquilla</name>
      <population>1064255</population>
    </city>
    <city>
      <name>Bogota</name>
      <population>5237635</population>
    </city>
    <city>
      <name>Cali</name>
      <population>1718871</population>
    </city>
    <language percentage="100">Spanish</language>
  </country>
  <country name="Comoros" population="569237" area="2170" />
  <country name="Congo" population="2527841" area="342000" />
  <country name="Cook Islands" population="19561" area="240" />
  <country name="Costa Rica" population="3463083" area="51100" />
  <country name="Cote dIvoire" population="14762445" area="322460" />
  <country name="Croatia" population="5004112" area="56538">
    <language percentage="96">Serbo-Croatian</language>
  </country>
  <country name="Cuba" population="10951334" area="110860">
    <city>
      <name>Havana</name>
      <population>2241000</population>
    </city>
    <language percentage="100">Spanish</language>
  </country>
  <country name="Cyprus" population="744609" area="9250" />
  <country name="Czech Republic" population="10321120" area="78703">
    <city>
      <name>Prague</name>
      <population>1215000</population>
    </city>
  </country>
  <country name="Denmark" population="5249632" area="43070">
    <city>
      <name>Copenhagen</name>
      <population>1358540</population>
    </city>
  </country>
  <country name="Djibouti" population="427642" area="22000" />
  <country name="Dominica" population="82926" area="750" />
  <country name="Dominican Republic" population="8088881" area="48730">
    <city>
      <name>Santo Domingo</name>
      <population>1400000</population>
    </city>
    <language percentage="100">Spanish</language>
  </country>
  <country name="Ecuador" population="11466291" area="283560">
    <city>
      <name>Quito</name>
      <population>1200000</population>
    </city>
    <city>
      <name>Guayaquil</name>
      <population>1300868</population>
    </city>
  </country>
  <country name="Egypt" population="63575108" area="1001450">
    <city>
      <name>El Giza</name>
      <population>1671000</population>
    </city>
    <city>
      <name>Alexandria</name>
      <population>2917000</population>
    </city>
    <city>
      <name>Cairo</name>
      <population>6053000</population>
    </city>
  </country>
  <country name="El Salvador" population="5828987" area="21040" />
  <country name="Equatorial Guinea" population="431282" area="28050" />
  <country name="Eritrea" population="3427883" area="121320" />
  <country name="Estonia" population="1459428" area="45100" />
  <country name="Ethiopia" population="57171664" area="1127127">
    <city>
      <name>Addis Ababa</name>
      <population>2316400</population>
    </city>
  </country>
  <country name="Falkland Islands" population="2374" area="12170">
    <language percentage="100">English</language>
  </country>
  <country name="Faroe Islands" population="43857" area="1400" />
  <country name="Fiji" population="782381" area="18270" />
  <country name="Finland" population="5105230" area="337030">
    <language percentage="6.3">Swedish</language>
    <language percentage="93.5">Finnish</language>
  </country>
  <country name="France" population="58317448" area="547030">
    <city>
      <name>Paris</name>
      <population>2152423</population>
    </city>
    <language percentage="100">French</language>
  </country>
  <country name="French Guiana" population="151187" area="91000">
    <language percentage="100">French</language>
  </country>
  <country name="French Polynesia" population="224911" area="4167" />
  <country name="Gabon" population="1172798" area="267670" />
  <country name="Gambia" population="1204984" area="11300" />
  <country name="Georgia" population="5219810" area="69700">
    <city>
      <name>Tbilisi</name>
      <population>1200000</population>
    </city>
    <language percentage="9">Russian</language>
    <language percentage="7">Armenian</language>
    <language percentage="6">Azeri</language>
    <language percentage="71">Georgian</language>
  </country>
  <country name="Germany" population="83536112" area="356910">
    <city>
      <name>Munchen</name>
      <population>1244676</population>
    </city>
    <city>
      <name>Muenchen</name>
      <population>1290079</population>
    </city>
    <city>
      <name>Berlin</name>
      <population>3472009</population>
    </city>
    <city>
      <name>Hamburg</name>
      <population>1705872</population>
    </city>
    <language percentage="100">German</language>
  </country>
  <country name="Ghana" population="17698272" area="238540" />
  <country name="Gibraltar" population="28765" area="6.5" />
  <country name="Greece" population="10538594" area="131940" />
  <country name="Greenland" population="58203" area="2175600" />
  <country name="Grenada" population="94961" area="340" />
  <country name="Guadeloupe" population="407768" area="1780">
    <language percentage="99">French</language>
  </country>
  <country name="Guam" population="156974" area="541.3" />
  <country name="Guatemala" population="11277614" area="108890">
    <language percentage="60">Spanish</language>
    <language percentage="40">Indian</language>
  </country>
  <country name="Guernsey" population="62920" area="194" />
  <country name="Guinea" population="7411981" area="245860">
    <language percentage="100">French</language>
  </country>
  <country name="Guinea-Bissau" population="1151330" area="36120" />
  <country name="Guyana" population="712091" area="214970" />
  <country name="Haiti" population="6731539" area="27750">
    <language percentage="10">French</language>
  </country>
  <country name="Holy See" population="840" area="0.44" />
  <country name="Honduras" population="5605193" area="112090" />
  <country name="Hungary" population="10002541" area="93030">
    <city>
      <name>Budapest</name>
      <population>2016000</population>
    </city>
    <language percentage="98.2">Hungarian</language>
  </country>
  <country name="Iceland" population="270292" area="103000">
    <language percentage="100">Icelandic</language>
  </country>
  <country name="India" population="952107712" area="3287590">
    <city>
      <name>Hyderabad</name>
      <population>3145939</population>
    </city>
    <city>
      <name>Ahmadabad</name>
      <population>2954526</population>
    </city>
    <city>
      <name>Surat</name>
      <population>1505872</population>
    </city>
    <city>
      <name>Vadodara</name>
      <population>1061598</population>
    </city>
    <city>
      <name>Bangalore</name>
      <population>3302296</population>
    </city>
    <city>
      <name>Bhopal</name>
      <population>1062771</population>
    </city>
    <city>
      <name>Indore</name>
      <population>1091674</population>
    </city>
    <city>
      <name>Mumbai</name>
      <population>9925891</population>
    </city>
    <city>
      <name>Nagpur</name>
      <population>1624752</population>
    </city>
    <city>
      <name>Pune</name>
      <population>1566651</population>
    </city>
    <city>
      <name>Kalyan</name>
      <population>1014557</population>
    </city>
    <city>
      <name>Ludhiana</name>
      <population>1042740</population>
    </city>
    <city>
      <name>Jaipur</name>
      <population>1458183</population>
    </city>
    <city>
      <name>Madras</name>
      <population>3841396</population>
    </city>
    <city>
      <name>Lucknow</name>
      <population>1619115</population>
    </city>
    <city>
      <name>Kanpur</name>
      <population>1879420</population>
    </city>
    <city>
      <name>Calcutta</name>
      <population>4399819</population>
    </city>
    <city>
      <name>New Delhi</name>
      <population>7206704</population>
    </city>
    <language percentage="30">Hindi</language>
  </country>
</countries>

这是我的XSL

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0"
            encoding="iso-8859-1" indent="yes"/>

<xsl:template match="/">
  <countries>
    <xsl:for-each select=".//country">
      <xsl:sort select="@population" order="descending" data-type="number"/>
      <country>
        <name>
          <xsl:value-of select="@name"/>
        </name>
        <population>
          <xsl:value-of select="@population"/>
        </population>
        <area>
          <xsl:value-of select="@area"/>
        </area>
        <cities>
             <xsl:for-each select=".//country/city">
              <city>
              <xsl:value-of select="@city"/>
              </city>
              </xsl:for-each>
        </cities>
      </country>
    </xsl:for-each>
  </countries>
</xsl:template>

</xsl:stylesheet>

在XSL之上,无法在输出xml中获得city名称。我认为我为城市做错了each-select

第二,如果任何国家的城市不止一个,如何按人口从高到低的顺序排序?

我可以知道吗,这是怎么回事。我是XSLT的新手

3 个答案:

答案 0 :(得分:0)

验证码:-

    <cities>
         <xsl:for-each select="city">
          <city>
          <xsl:value-of select="name"/>
          </city>
          </xsl:for-each>
    </cities>

答案 1 :(得分:0)

因为您已经位于.//country下,所以无需在第二个for-each下再次指定.// country。

<cities>
             <xsl:for-each select="city">
              <city>
              <xsl:value-of select="name"/>
              </city>
              </xsl:for-each>
        </cities>

答案 2 :(得分:0)

1)。如上所述,@ Ansari建议您已经在country node()上,因此不需要内部for-each部分.//country,因为它正在当前上下文中搜索country这就是为什么您的输出中没有任何关于城市的条目的原因。 2)。您可以像上次在国家/地区中那样在内部循环中使用xsl:sort,请参考下面的代码,希望它可以让您知道在哪里出错:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

    <xsl:template match="/">
        <countries>
            <xsl:for-each select="//country">
                <xsl:sort select="@population" order="descending" data-type="number"/>
                <country>
                    <name>
                        <xsl:value-of select="@name"/>
                    </name>
                    <population>
                        <xsl:value-of select="@population"/>
                    </population>
                    <area>
                        <xsl:value-of select="@area"/>
                    </area>
                    <cities>
                        <xsl:for-each select="city">
                            <xsl:sort select="population" order="descending"/>
                            <city>
                                <!--<xsl:if test="population != ''">
                                    <xsl:attribute name="population" select="population"/>
                                </xsl:if>-->
                                <xsl:value-of select="name"/>
                            </city>
                        </xsl:for-each>
                    </cities>
                </country>
            </xsl:for-each>
        </countries>
    </xsl:template>

</xsl:stylesheet>

您可以在这里查看结果:http://xsltransform.net/6qjwab7