1

除了 xpath 验证之外,第一次尝试使用 XML。我有一个家庭作业,我应该链接一个 XML 和一个 XSL 文档,以便 XML 在 firefox v25 或更高版本的浏览器中打开时显示如下:

山名:珠穆朗玛峰
猪 拉丁名:ountMa verestEa
山名:拉尼尔
山 山名:圣海伦斯
山 山名:华盛顿
山猪 拉丁名:ountMa ashingtonWa
山名:
邦内尔山猪 拉丁名: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>

由于我将它们保存在同一个文件夹中,我认为 match="/" 会很好,然后冒险为每个 for-each 选择,我选择山,然后在 value-of select 下,名称是一个元素山下。但是,当我打开我的 XML 文档时,我看不到这反映在任何地方。我还没有进入猪拉丁部分,因为首先我希望山名起作用。如何让这两个文件互相看到?

我尝试过的事情:添加 href 行,例如 ?xml-stylesheet type="text/xsl" href="Asg04.xml"? 以及 Asg04XST.xsl

4

1 回答 1

1

这是更新的 xml & 和样式表以获得所需的输出:

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

输出

在此处输入图像描述

于 2016-10-17T02:18:13.567 回答