向表xslt,xml

时间:2017-11-09 19:07:03

标签: xml xslt

我试图添加一个能够进行逻辑计算的参数(简单的加法减法),我似乎无法显示这些项目。

XSLT代码 我想弄清楚建筑物是否被毁坏,如果不计算建筑物,分机已经站立的时间。

    <h2>Overview</h2>
    <table border="1">

      <tr>
        <th>Wonder Name</th>
        <th>Location</th>
        <th>Height</th>
        <th>Years Standing</th>
      </tr>


      <!-- ** XML2e ** -->
      <!-- to follow the book's example, see this and the sections below -->
      <xsl:apply-templates select="ancient_wonders/wonder">
        <xsl:sort select="height" order="descending" data-type="number" />
      </xsl:apply-templates>


      <td valign="top">
        <xsl:choose>
          <xsl:when test="history/year_destroyed !=0">

            <xsl:choose>
              <xsl:when test="history/year_destroyed/@era = 'BC'">
                <xsl:value-of select="history/year_built - history/year_destroyed"></xsl:value-of>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="history/year_built + history/year_destroyed - 1"/>
              </xsl:otherwise>
            </xsl:choose>

          </xsl:when>

        </xsl:choose>
      </td>

XML CODE

<?xml version="1.0"?>
<!-- Note: For many of the wonders, the experts do not agree on precise dates or construction dimensions.  In these cases, I chose a year or dimension in the middle of the range so all attributes could be numeric. -->
<ancient_wonders>
    <wonder>
        <name language="English">Colossus of Rhodes</name>
        <name language="Greek">Κολοσσός της Ρόδου</name>
        <location>Rhodes, Greece</location>
        <height units="feet">107</height>
        <history>
            <year_built era="BC">282</year_built>
            <year_destroyed era="BC">226</year_destroyed>
            <how_destroyed>earthquake</how_destroyed>
            <story>
        In 294 BC, the people of the island of Rhodes began building a colossal statue of the sun god Helios.  They believed that it was because of his blessings that they were able to withstand a long siege on the island and emerge victorious.<para/>The Colossus was built with bronze, reinforced with iron, and weighted with stones.  While it is often depicted straddling Mandrákion harbor, this is now considered technically impossible; and therefore, it likely stood beside the harbor.<para/>The statue was toppled by an earthquake in 226 BC.  It snapped at the knees and fell over on to the land.  The Oracle of Delphi suggested that it fell because the people of Rhodes had offended Helios, and they decided not to rebuild it.<para/>The statue remained on the ground until 654 AD., and even broken, it was so impressive that many traveled to see it.
      </story>
        </history>
        <main_image file="colossus.jpg" w="528" h="349"/>
        <source sectionid="101" newspaperid="21"/>
    </wonder>
    <wonder>
        <name language="English">Great Pyramid of Giza</name>
        <location>Giza, Egypt</location>
        <height units="feet">455</height>
        <history>
            <year_built era="BC">2570</year_built>
            <story>
        It is believed to have taken 20 years and as many as 100,000 workers to complete the Great Pyramid, which was built as a tomb for the 4th dynasty Egyptian pharaoh named Khufu.  It was built as part of a complex that included temples and many other pyramids.<para/>The outermost stones of the pyramid were highly polished white limestone, which were eventually loosened by an earthquake more than 600 years ago, and were removed to help build cities and mosques.  When these casing stones were present, some believe that the pyramid was so large and bright that it could be seen from the moon.<para/>There is much speculation about the construction methodologies and intentions of the pyramid, including references to the moon, the Orion constellation, continental gravity, and more.  What is known, however, is that the four sides of the base are more than 700 feet long, and differ in length by no more than 8 inches.  Each side of the pyramid is almost perfectly aligned with the four cardinal points of the compass.  And, the pyramid's dimensions covert to a ratio that equates to 2π with nearly perfect accuracy.
      </story>
        </history>
        <main_image file="pyramid.jpg" w="528" h="349"/>
        <source sectionid="123" newspaperid="71"/>
        <source sectionid="21" newspaperid="42"/>
    </wonder>
    <wonder>
        <name language="English">Hanging Gardens of Babylon</name>
        <location>Al Hillah, Iraq</location>
        <height units="feet">0</height>
        <history>
            <year_built era="BC">600</year_built>
            <year_destroyed era="BC">226</year_destroyed>
            <how_destroyed>earthquake</how_destroyed>
            <story>
        The first and most interesting fact about the gardens is that there is significant controversy about whether the gardens existed at all.<para/>Whether they did or did not exist, there are many elaborate accounts and poetic descriptions of the gardens.  They are generally believed to have been built by King Nebuchadrezzar II for his wife because she missed the gardens of her home.  Some research suggests that it was an elaborate series of rooftop gardens.  Another theory suggests that the gardens were built in the walls of the royal palace.<para/>Babylon, being in the middle of the desert, had no such natural greenery, however; calling into question of how such lushness would have survived in the dryness of the desert.  Recent excavations have suggested the use of an exceptional system of irrigation using a cellar well in conjunction with a chain pump to raise the water to the gardens.  Still, other theories even suggest that the gardens weren't in Babylon at all.  Rather, that they were constructed by Sennacherib at the eastern bank of the Tigris river.<para/>Regardless of the final outcome, it is interesting to note that the imagination of the poets and ancient historians have created one of the Wonders of the World.
      </story>
        </history>
        <main_image file="gardens.jpg" w="528" h="349"/>
        <source sectionid="11" newspaperid="24"/>
        <source sectionid="18" newspaperid="151"/>
    </wonder>
    <wonder>
        <name language="English">Statue of Zeus at Olympia</name>
        <name language="Greek">Δίας μυθολογία</name>
        <location>Olympia, Greece</location>
        <height units="feet">39</height>
        <history>
            <year_built era="BC">430</year_built>
            <year_destroyed era="AD">426</year_destroyed>
            <how_destroyed>fire</how_destroyed>
            <story>Carved from ivory, on a throne of cedarwood, the statue was created by the Greek sculptor Phidias around 430 BC.  In its right hand was a life-size statue of Nike, the goddess of victory, and in its left was a large sceptre topped with an eagle.  The statue, nearly 40 feet tall, was said to fill the entire width of the one of the aisles of the Temple of Zeus, and its head nearly scraped the temple's ceiling.<para/>In 426 AD, the temple was destroyed and many historians believe that the statue was destroyed at that time. Others believe, however, that the statue was moved to Constantinople (now Istanbul) and was destroyed about 50 years later.<para/>Although many copies of the statue were made during its 800+ year lifespan, none are known to have survived.</story>
        </history>
        <main_image file="zeus.jpg" w="528" h="349"/>
    </wonder>
    <wonder>
        <name language="English">Temple of Artemis at Ephesus</name>
        <name language="Greek">Ἀρτεμίσιον</name>
        <location>Ephesus, Turkey</location>
        <height units="feet">60</height>
        <history>
            <year_built era="BC">550</year_built>
            <year_destroyed era="BC">356</year_destroyed>
            <how_destroyed>fire</how_destroyed>
            <story>The Temple of Artemis at Ephesus was built and rebuilt over the course of many years.  The most famous was built in about 550 BC and was funded by King Croesus of Lydia.<para/>Unlike many other temples of its time, it was built of marble and glittered with gold.  The temple's foundation was over 180 by 350 feet and its steps led to a high platform on which stood more than one hundred beautifully sculptured columns that supported its roof.  At least twice the size of the Parthenon, it was so breathtaking it was said to rise to the clouds.<para/>The temple housed many incredible works of art, but the most famous was the statue of Artemis herself.  Although the original no longer exists, it was thought to have been made of gold and other beautiful materials.  Her legs were covered with animals and bees and the top of the body covered with breasts symbolizing fertility.<para/>Although destroyed and rebuilt many times, and ultimately left to be destroyed by man, nature and neglect, the most famous version of the temple was destroyed by arson committed by a man named Herostratus on the night Alexander the Great was born.</story>
        </history>
        <main_image file="artemis.jpg" w="528" h="349"/>
        <source sectionid="92" newspaperid="19"/>
    </wonder>
    <wonder>
        <name language="English">Mausoleum at Halicarnassus</name>
        <name language="Greek">Μαυσωλεῖον Ἁλικαρνασσεύς</name>
        <location>Bodrum, Turkey</location>
        <height units="feet">135</height>
        <history>
            <year_built era="BC">352</year_built>
            <year_destroyed era="AD">1404</year_destroyed>
            <how_destroyed>earthquake</how_destroyed>
            <story>The monument was built as a tomb for Mausolus, a governor in the Persian Empire. Sparing no expense, his widowed wife Artemisia (who was also his sister as was considered customary) commissioned well-known Greek architects and artists to design the tomb and sculptural reliefs for each of the four sides of the building.<para/>The monument was almost square, nearly 14 stories tall, and its base was more than 10,000 square feet. Each side of the tomb had nine massive columns, and the top was a stepped pyramid on which stood by a four-horse marble chariot in which there were images of Mausolus and Artemisia.<para/>The monument stood for nearly 17 centuries and was eventually destroyed by a series of earthquakes. And, like the Lighthouse of Alexandria, the tomb was so famous that Mausolus's name has become the root word for large tombs in many languages (for example, "mausoleo" in Spanish).</story>
        </history>
        <main_image file="mausoleum.jpg" w="528" h="349"/>
        <source sectionid="141" newspaperid="21"/>
        <source sectionid="2" newspaperid="19"/>
    </wonder>
    <wonder>
        <name language="English">Lighthouse of Alexandria</name>
        <name language="Greek">ὁ Φάρος τῆς Ἀλεξανδρείας</name>
        <location>Alexandria, Egypt</location>
        <height units="feet">384</height>
        <history>
            <year_built era="BC">280</year_built>
            <year_destroyed era="AD">1323</year_destroyed>
            <how_destroyed>earthquake</how_destroyed>
            <story>
        Located on the island of Pharos in the harbor of Alexandria, the lighthouse may be the most famous lighthouse in history.  The lighthouse was very different than modern lighthouses in that it was built in three stages, all sloping inward.  Built of marble blocks with lead mortar; the lowest was square, the next octagonal, and the top cylindrical.  Within the lighthouse was a ramp and "dumbwaiter" that allowed wood to be transported to the fire that burned at night.<para/>Inside the open top of the lighthouse was a large curved mirror that was used to reflect the sunlight during the day and the fire at night.  It is said that mariners could see the light up to 35 miles away.  Legends have it that the light was so bright it could be used to burn enemy ships.<para/>There are many stories about its ultimate demise, however, the most likely cause is believed to be as many as 22 earthquakes between 320 AD and 1323 AD which led to its decommission.<para/>The Lighthouse of Alexandria was so famous in ancient times that the island on which it stood, Pharos, became the root word for lighthouse in many languages (for example, "phare" in French).
      </story>
        </history>
        <main_image file="lighthouse.jpg" w="528" h="349"/>
        <source sectionid="112" newspaperid="53"/>
    </wonder>
</ancient_wonders>

截至目前,使用此代码时,列无法显示建筑物所处的年数。

1 个答案:

答案 0 :(得分:0)

不确定您共享的XSLT代码是否是完整XSLT的片段,但计算部分需要在<xsl:template>范围内才能生效。使用代码段并将其包装在模板中后,将计算并正确显示值。

下面是完整的XSLT和输出。

<xsl:template match="ancient_wonders">
    <h2>Overview</h2>
    <table border="1">
        <tr>
            <th>Wonder Name</th>
            <th>Location</th>
            <th>Height</th>
            <th>Years Standing</th>
        </tr>
        <xsl:for-each select="wonder">
            <xsl:sort select="height" order="descending" data-type="number" />
            <tr>
                <td><xsl:value-of select="name[@language='English']" /></td>
                <td><xsl:value-of select="location" /></td>
                <td><xsl:value-of select="concat(height, ' ', height/@units)" /></td>
                <td>
                    <xsl:choose>
                        <xsl:when test="history/year_destroyed != 0">
                            <xsl:choose>
                                <xsl:when test="history/year_destroyed/@era = 'BC'">
                                    <xsl:value-of select="history/year_built - history/year_destroyed" />
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="history/year_built + history/year_destroyed - 1" />
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>Ancient Wonder is still going strong. Please do visit.</xsl:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

输出

<h2>Overview</h2>
<table border="1">
    <tr>
        <th>Wonder Name</th>
        <th>Location</th>
        <th>Height</th>
        <th>Years Standing</th>
    </tr>
    <tr>
        <td>Great Pyramid of Giza</td>
        <td>Giza, Egypt</td>
        <td>455 feet</td>
        <td>Ancient Wonder is still going strong. Please do visit.</td>
    </tr>
    <tr>
        <td>Lighthouse of Alexandria</td>
        <td>Alexandria, Egypt</td>
        <td>384 feet</td>
        <td>1602</td>
    </tr>
    <tr>
        <td>Mausoleum at Halicarnassus</td>
        <td>Bodrum, Turkey</td>
        <td>135 feet</td>
        <td>1755</td>
    </tr>
    <tr>
        <td>Colossus of Rhodes</td>
        <td>Rhodes, Greece</td>
        <td>107 feet</td>
        <td>56</td>
    </tr>
    <tr>
        <td>Temple of Artemis at Ephesus</td>
        <td>Ephesus, Turkey</td>
        <td>60 feet</td>
        <td>194</td>
    </tr>
    <tr>
        <td>Statue of Zeus at Olympia</td>
        <td>Olympia, Greece</td>
        <td>39 feet</td>
        <td>855</td>
    </tr>
    <tr>
        <td>Hanging Gardens of Babylon</td>
        <td>Al Hillah, Iraq</td>
        <td>0 feet</td>
        <td>374</td>
    </tr>
</table>