Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT / XSD question 1

Status
Not open for further replies.

lithous

Programmer
Joined
Jul 20, 2004
Messages
1
Location
US
I am pretty new to XSLT (did one small project with it) and now I am looking to do another with XML data from a third party and I noticed they have xmlns:xsd and xmlns:xsi attributes in one of the tags. This is really throwing me off...

Other than the xmlns attributes it looks like a typical XML files I have transformed before...

1. So why would the need for those xmlns attributes arise (i.e. why are they necessary here)?

2. And how would I modify my XSL to transform the file?

-----------------

Here is the XML sent from the third party:

<?xml version="1.0" encoding="utf-8" ?>
<WeatherFeed xmlns:xsd=" xmlns:xsi=" xmlns="h
ttp://<internal>

</internal>
<Location> Alexandria, VA, United States </Location>
<RecordedAt> Alexandria, VA, United States </RecordedAt>
<Updated> 1151 AM EDT TUE JUL 20 2004 </Updated>
<Conditions> Partly Cloudy </Conditions>
<Visibility> 10 Mi </Visibility>
<Temp> 82 F </Temp>
<Humidity> 48 % </Humidity>
<Wind>N 0 MPH</Wind>
<Barometer> 29.96 in. </Barometer>
<Dewpoint> 61 F</Dewpoint>
<HeatIndex> 83 F</HeatIndex>
<WindChill> 82 F</WindChill>
<PoweredBy>Company</PoweredBy>
<Copyright> Canadian forecasts are obtained from and are copyright of Environment Canada. All other data is derived from
the NWS and other public domain sources. </Copyright>
</WeatherFeed>

-------------------

Here is my XSL which works when the xmlns:xsd attributes aren't present in the <WeatherFeed> tag:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl=" version='1.0'>

<xsl:template match="/">

<html>
<head>
<title>
<xsl:value-of select='//Location'/>
</title>
</head>
<body bgcolor="blue">
<table width="100%">
<TR><td ><FONT SIZE="+3">
<xsl:for-each select="//Location">
Weather for <xsl:apply-templates select='.'/>
</xsl:for-each></FONT></td>
</TR>
<TR><td ><FONT SIZE="+2">
<xsl:for-each select="//Conditions">
Conditions: <xsl:value-of select='.'/>
</xsl:for-each></FONT></td>
</TR>
<TR><td ><FONT SIZE="+2">
<xsl:for-each select="//Temp">
Temp: <xsl:apply-templates select="."/>
</xsl:for-each></FONT></td>
</TR>
<TR><td ><FONT SIZE="+2">
<xsl:for-each select="//Barometer">
Barometer: <xsl:value-of select='.'/>
</xsl:for-each></FONT></td>
</TR>
<TR><td ><FONT SIZE="+2">
<xsl:for-each select="//PoweredBy">
<xsl:apply-templates select="."/>
</xsl:for-each></FONT></td>
</TR>
</table>
</body>
</html>

</xsl:template>


<xsl:template match='Location'>

<xsl:variable name="location">
<xsl:value-of select="."/>
</xsl:variable>

<xsl:variable name="city">
<xsl:value-of select="substring-before($location,',')"/>
</xsl:variable>

<xsl:variable name="stateAbbr">
<xsl:value-of select="substring-before(substring-after($location,','), ',')"/>
</xsl:variable>

<font color="red"><i><xsl:value-of select='$city'/>, <xsl:value-of select='$stateAbbr'/></i></font>
</xsl:template>

<xsl:template match='Temp'>
<font color="red"><i><xsl:value-of select='.'/></i></font>
</xsl:template>

<xsl:template match='PoweredBy'>
<xsl:variable name="url"><xsl:value-of select="."/></xsl:variable>
<xsl:variable name="href">
<xsl:value-of select="substring-before(substring-after($url,' '), ' ')"/>
</xsl:variable>

<a>
<xsl:attribute name="href"> select="$href"/></xsl:attribute>
<font color="white"><xsl:value-of select="$href"/></font>
</a>

</xsl:template>

</xsl:stylesheet>


TIA
 
Not sure if this result is from a Web Service/SOAP. If so, this maybe why the xsd and xsi prefixes are being defined. The xsi is usually used with schemaLocation to tell the parser where the schema is located for this XML file. The biggest problem you have encountered is the default namespace

xmlns="
They are doing this to prevent name collision. Because of the namespace declaration, you need to replicate this definition in the xslt also. The namespace is part of the document. So in the xslt, you need to add the following:

<xsl:stylesheet xmlns:xsl=" xmlns:weather=" version='1.0'>

and the prefix all XPath with weather:

Just as an example.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:weather="[URL unfurl="true"]http://www.weatherroom.com"[/URL] version='1.0'>

<xsl:template match="/">

<html>
    <head>
        <title>
             <xsl:value-of select='//weather:Location'/>
        </title>
    </head>
<body bgcolor="blue">     
    <table width="100%">
       <TR><td ><FONT SIZE="+3">
            <xsl:for-each select="//weather:Location">
        Weather for <xsl:apply-templates select='.'/>
                </xsl:for-each></FONT></td>
    </TR>      
    <TR><td ><FONT SIZE="+2">
       <xsl:for-each select="//weather:Conditions">
           Conditions: <xsl:value-of select='.'/>
       </xsl:for-each></FONT></td>
    </TR>
    <TR><td ><FONT SIZE="+2">
       <xsl:for-each select="//weather:Temp">
           Temp: <xsl:apply-templates select="."/>
       </xsl:for-each></FONT></td>
    </TR>
    <TR><td ><FONT SIZE="+2">
       <xsl:for-each select="//weather:Barometer">
           Barometer: <xsl:value-of select='.'/>
       </xsl:for-each></FONT></td>
    </TR>
    <TR><td ><FONT SIZE="+2">
       <xsl:for-each select="//weather:PoweredBy">
           <xsl:apply-templates select="."/>
       </xsl:for-each></FONT></td>
    </TR>
    </table>
</body>
</html>

</xsl:template>


<xsl:template match='weather:Location'>

    <xsl:variable name="location">
    <xsl:value-of select="."/>
    </xsl:variable>

    <xsl:variable name="city">
    <xsl:value-of select="substring-before($location,',')"/>
    </xsl:variable>

    <xsl:variable name="stateAbbr">
    <xsl:value-of select="substring-before(substring-after($location,','), ',')"/>
    </xsl:variable>

    <font color="red"><i><xsl:value-of select='$city'/>, <xsl:value-of select='$stateAbbr'/></i></font>
</xsl:template>

<xsl:template match='Temp'>
<font color="red"><i><xsl:value-of select='.'/></i></font>
</xsl:template>

<xsl:template match='PoweredBy'>
    <xsl:variable name="url"><xsl:value-of select="."/></xsl:variable>
    <xsl:variable name="href">
        <xsl:value-of select="substring-before(substring-after($url,' '), ' ')"/>
    </xsl:variable>

    <a>
    <xsl:attribute name="href">[URL unfurl="true"]http://<xsl:value-of[/URL] select="$href"/></xsl:attribute>
    <font color="white"><xsl:value-of select="$href"/></font>
    </a>

</xsl:template>

</xsl:stylesheet>

Hope this helps

-jay
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top