john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

xsl xml

XSLT is a set of instructions to transform an xml file into something else (most often html) XML is a fancy way of describing a text document that stores data in structured fashion HTML is the most common way of formatting a document to present data (for web browsers)


example source XML file

Empire Burlesque Bob Dylan USA Columbia 10.90 1985 Hide your heart Dolly Parton UK CBS Records 9.90 1988 Greatest Hits Dolly Parton USA RCA 9.90 1982 Still got the blues Gary Moore UK Virgin records 10.20 1990


Since an XSL style sheet is an XML document itself it always begins with the XML declaration:

then we indicate that this document is of type XSL (and where to understand what XSL is defined -> which means going onto that website!)


Thus a very simple example would be:

<xsl:template match="/">

      <xsl:value-of select="catalog/cd/title"/>

</xsl:template>


AND THE RESULT IS:

Empire Burlesque


only one item because we have not done any looping or logic, just displaying one item... (Note the value-of select gives a path that is forward slash '/' delimited)

IF WE MODIFY IT A LITTLE BIT TO INCLUDE A for-each LOOP...

-


AND THE RESULT IS:

Empire Burlesque - Bob Dylan Hide your heart - Dolly Parton Greatest Hits - Dolly Parton Still got the blues - Gary Moore


NEXT WE HAVE MORE ADVANCED FORMATTING

<xsl:attribute name="title">
    <xsl:value-of select="title"/>
</xsl:attribute>

<xsl:text> *-* </xsl:text>

<xsl:attribute name="artist">
    <xsl:value-of select="artist"/>
</xsl:attribute>

<br />


outputs the TRANSFORMED into an XML doc, w/ indentation! The element inserts text into the XML format would display all data in the chosen element!

In this particular case we are trying to create an XML document from a RSS feed The "indent" means the output XML will have indentation, I'm not sure about the omit-xml... http://www.w3schools.com/xsl/el_output.asp

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

I believe this starts at the "root" of the XML document and then finds the first rss/channel, It then makes a "div" out of each channel

It then labels the feed with the name & link & description and then gets the next "item"

ABOVE you can see how it creates an unnumbered list from each Item

<xsl:template match="item">
    <xsl:variable name="item_link" select="link"/>
    <xsl:variable name="item_title" select="description"/>
    <li>
        <a href="{$item_link}" title="{$item_title}"><xsl:value-of select="title"/></a>
    </li>

XML link: http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=GBP&ToCurrency=USD

1 GBP = USD


THE "source" RSS FEED DATA (which could be pulled regularly?)

http://coinmill.com/rss/GBP_USD.xml Coinmill.com Currency Rates

http://coinmill.com/ Current rates and calculator for currencies en Tue, 27 Jan 2009 00:00:00 EST Wed, 28 Jan 2009 04:37:47 EST

Pound Sterling and United States Dollar http://coinmill.com/GBP_USD.html <![CDATA[ 1.00 GBP = 1.40 USD
1.00 USD = 0.71 GBP
Converter -- Rate Chart ]]>
Tue, 27 Jan 2009 00:00:00 EST Coinmill.com GBP/USD 2009-01-27


THE WHOLE XSL EXAMPLE THAT TURNS AN XML INTO A BULLET LIST OF LINKS

<xsl:template match="rss/channel">
    <xsl:variable name="link" select="link"/>
    <xsl:variable name="description" select="description"/>

    <ul><xsl:apply-templates select="item"/></ul>
</xsl:template>

<xsl:template match="item">
    <xsl:variable name="item_link" select="link"/>
    <xsl:variable name="item_title" select="description"/>
    <li>
        <a href="{$item_link}" title="{$item_title}"><xsl:value-of select="title"/></a>
    </li>
</xsl:template>


myname today money

//defines the root of the template

//to make things easy on ourselves variableValue

The xsl:variable instruction creates a variable. NAME attribute identifies the variable's name The value can be specified either as the xsl:variable element's contents (e.g. variableValue) or as the value of an optional select attribute in the xsl:variable element's start-tag.

ALSO you see that the variable select can call a function...

VARIABLES!!!!!!!!!! http://www.xml.com/pub/a/2001/02/07/trxml9.html

//or it could be something specific, note that the html is mixed with XSL to produce formatting

{$descriptiveName}


DEDUPLICATION

http://www.dpawson.co.uk/xsl/sect2/N2696.html#d3983e16


  • « windows reload registry user HKCU
  • wss 3 custom page template »

Published

Feb 6, 2010

Category

research

~560 words

Tags

  • research 199
  • xml 22
  • xsl 1