1

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

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
Pix81
  • 585
  • 3
  • 15
  • 33
  • 1
    See if this helps: http://stackoverflow.com/questions/24629700/testing-xslt-code-using-your-browser/24632054#24632054 – michael.hor257k Oct 16 '16 at 22:29
  • That worked! I just messed up the headers this whole time – Pix81 Oct 17 '16 at 02:12
  • 1
    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: [mcve]. – michael.hor257k Oct 22 '16 at 21:47

1 Answers1

1

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

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Thank you so much! – Pix81 Oct 22 '16 at 18:04
  • 1
    @JuliePixie, glad to know that answer is helpful. Please consider accepting it an answer. – Rao Oct 23 '16 at 01:47
  • Ah, I voted on it? Is there another way to accept? Don't use this site very much yet – Pix81 Oct 23 '16 at 02:21
  • @JuliePixie, no problem. Please check http://stackoverflow.com/tour or http://stackoverflow.com/help/someone-answers to accept it as answer. In order to vote, minimum 15 reputation, you may do it later if you wish ;) – Rao Oct 23 '16 at 02:32
  • @JuliePixie: Please [**accept**](http://meta.stackoverflow.com/q/5234/234215) this answer if it's helped. You might also consider revisiting some of your [past questions](http://stackoverflow.com/users/5031905/julie-pixie?tab=questions) and accepting helpful answers for those as well. Thanks. – kjhughes Oct 23 '16 at 23:19
  • I dont see the check mark I'd need to accept, I guess it shows up when i hit a 15 reputation? – Pix81 Oct 24 '16 at 04:51
  • Click on the tick mark below the voting options as shown [here](http://s000.tinyupload.com/?file_id=01806915598159278818) for the answer. The one posted question will have the option to accept irrespective of reputation. – Rao Oct 24 '16 at 04:55