1
votes

first time trying to use XML besides xpath validation. I have a homework assignment where I'm supposed to link an XML and an XSL document so that the XML, when opened in a firefox v25 or later browser displays like such:

Mountain Name: Mount Everest
Pig Latin Name: ountMa verestEa
Mountain Name: Mount Ranier
Mountain Name: Mount St. Helens
Mountain Name: Mount Washington
Pig Latin Name: ountMa ashingtonWa
Mountain Name: Mount Bonnell
Pig Latin Name: ountMa onnellBa
Mountain Name: Mount Vesuvius
Pig Latin Name: ountMa esuviusVa
Mountain Name: Mount Etna
Pig Latin Name: ountMa tnaEa

My XML code is as follows for the file Asg04XST.xml. I have saved this on my desktop in a folder called 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>

Then, I've created this XSL file in the same folder, called 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>

Since I saved them in the same folder, I would think match="/" would be fine, then would venture that for each for-each select, I'm selecting mountain and then under value-of select, name which is an element under mountain. However, when I open my XML document, I don't see this reflected anywhere. I haven't gotten to the pig latin section because first I want the Mountain Name to work. How do I get these two files to see each other?

Things I've tried: adding the href line such as ?xml-stylesheet type="text/xsl" href="Asg04.xml"? as well as Asg04XST.xsl

1
That worked! I just messed up the headers this whole time - Julie Pixie
I have rolled back your question to what it was when it was answered. Please post a new question describing your new problem. Make sure to provide all the code necessary to reproduce the issue, including the expected result - see: minimal reproducible example. - michael.hor257k

1 Answers

1
votes

Here is the updated xml & and style sheet to get the desired output:

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

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

Output

enter image description here