第一次尝试使用XML而不是xpath验证。我有一个家庭作业,我应该链接XML和XSL文档,以便在firefox v25或更高版本的浏览器中打开XML时显示如下:
山名:珠穆朗玛峰
猪拉丁名:ountMa verestEa
山名:Mount Ranier
山名:圣海伦山
山名:华盛顿山
猪拉丁名:ountMa ashingtonWa
山名:Mount Bonnell
猪拉丁名:ountMa onnellBa
山名:维苏威火山
猪拉丁名:ountMa esuviusVa
山名:埃特纳火山
猪拉丁名:ountMa tnaEa
对于文件Asg04XST.xml,我的XML代码如下。我已将其保存在桌面上名为Asg04的文件夹中:
<FamousMountains>
<mountain>
<name language="English">Mount Everest</name>
<name language="PigLatin">ountMa verestEa</name>
<location>Nepal</location>
<height units="feet">29035</height>
</mountain>
<mountain>
<name language="English">Mount Ranier</name>
<location>Washington</location>
<height units="feet">14411</height>
</mountain>
<mountain>
<name language="English">Mount St. Helens</name>
<location>Washington</location>
<height units="feet">8364</height>
</mountain>
<mountain>
<name language="English">Mount Washington</name>
<name language="PigLatin">ountMa ashingtonWa</name>
<location>New Hampshire</location>
<height units="feet">6288</height>
</mountain>
<mountain>
<name language="English">Mount Bonnell</name>
<name language="PigLatin">ountMa onnellBa</name>
<location>Austin</location>
<height units="feet">800</height>
</mountain>
<mountain>
<name language="English">Mount Vesuvius</name>
<name language="PigLatin">ountMa esuviusVa</name>
<location>Italy</location>
<height units="feet">4203</height>
</mountain>
<mountain>
<name language="English">Mount Etna</name>
<name language="PigLatin">ountMa tnaEa</name>
<location>Sicily</location>
<height units="feet">10922</height>
</mountain>
</FamousMountains>
然后,我在同一个文件夹中创建了这个XSL文件,名为Asg04.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" />
<xsl:template match="FamousMountains">
<html>
<head><title><h3>Julie Laursen</h3></title></head>
<body>
<xsl:for-each select="mountain">
Mountain Name: <xsl:value-of select="name"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
由于我将它们保存在同一个文件夹中,我认为匹配=&#34; /&#34;没关系,然后冒险为每个选择,我选择山,然后选择值,名称是山下的元素。但是,当我打开我的XML文档时,我不会在任何地方看到这种情况。我还没到猪拉丁区,因为我首先想要山名工作。如何让这两个文件相互看到?
我尝试过的事情:添加href行如 ?xml-stylesheet type =&#34; text / xsl&#34; HREF =&#34; Asg04.xml&#34 ;? 以及Asg04XST.xsl
答案 0 :(得分:1)
这是更新后的xml&amp;和样式表以获得所需的输出:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?>
<FamousMountains>
<mountain>
<name language="English">Mount Everest</name>
<name language="PigLatin">ountMa verestEa</name>
<location>Nepal</location>
<height units="feet">29035</height>
</mountain>
<mountain>
<name language="English">Mount Ranier</name>
<location>Washington</location>
<height units="feet">14411</height>
</mountain>
<mountain>
<name language="English">Mount St. Helens</name>
<location>Washington</location>
<height units="feet">8364</height>
</mountain>
<mountain>
<name language="English">Mount Washington</name>
<name language="PigLatin">ountMa ashingtonWa</name>
<location>New Hampshire</location>
<height units="feet">6288</height>
</mountain>
<mountain>
<name language="English">Mount Bonnell</name>
<name language="PigLatin">ountMa onnellBa</name>
<location>Austin</location>
<height units="feet">800</height>
</mountain>
<mountain>
<name language="English">Mount Vesuvius</name>
<name language="PigLatin">ountMa esuviusVa</name>
<location>Italy</location>
<height units="feet">4203</height>
</mountain>
<mountain>
<name language="English">Mount Etna</name>
<name language="PigLatin">ountMa tnaEa</name>
<location>Sicily</location>
<height units="feet">10922</height>
</mountain>
</FamousMountains>
mystylesheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html" />
<xsl:template match="/">
<html>
<head><title>Julie Laursen</title></head>
<body>
<h3>Julie Laursen</h3>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="mountain">
<xsl:for-each select="name">
<xsl:if test="@language='English'">
Mountain Name: <xsl:value-of select="."/><br/>
</xsl:if>
<xsl:if test="@language='PigLatin'">
Pig Latin Name: <xsl:value-of select="."/><br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<强>输出强>