2 Replies - 511 Views - Last Post: 07 May 2012 - 12:01 PM

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

XML and XSLT in the real world environment - automatic transformation

Posted 07 May 2012 - 10:10 AM

OK so I'm using Neatbeans IDE and have created an XML document and and XSLT that I am using to transform the XML into html.

<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : catalogue.xml
    Created on : 07 May 2012, 17:55
-->

<catalog>
	<cd>
		<title>Empire Burlesque</title>
		<artist>Bob Dylan</artist>
		<country>USA</country>
		<company>Columbia</company>
		<price>10.90</price>
		<year>1985</year>
	</cd>
</catalog>


<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : newstylesheet.xsl
    Created on : 07 May 2012, 17:56
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>

    <!-- TODO customize transformation rules 
         syntax recommendation http://www.w3.org/TR/xslt 
    -->
    <xsl:template match="/">
        <html>
            <head>
                <title>newstylesheet.xsl</title>
            </head>
            <body>
                <h2>My CD Collection</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Title</th>
                        <th>Artist</th>
                    </tr>
                    <xsl:for-each select="catalog/cd">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td>
                                <xsl:value-of select="artist"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>



I use the IDE to perform the transformation and I get a html document containing a table with my data as expected.

The things is if I host this html and decide to add an additional CD to the collection I have to perform the transformation again. Is it possible for the html to automatically update. Why do I have to keep transforming the XML document?

Is This A Good Question/Topic? 0
  • +

Replies To: XML and XSLT in the real world environment - automatic transformation

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: XML and XSLT in the real world environment - automatic transformation

Posted 07 May 2012 - 11:00 AM

if you write a programme that
- automatically connects to the server
- compares the modification times of the HTML and the XML file
- does the XSL Transformation
- writes the result to the server

then you can automatically update the HTML
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: XML and XSLT in the real world environment - automatic transformation

Posted 07 May 2012 - 12:01 PM

Neat. I'd never tried that in netbeans...

The point is that the XML document is expected to change all the time. It's the result of a query against a server. The formatting of that result is reasonably consistent, so taking the dynamic XML that you have to write code for anyhow and applying an XSLT stylesheet is often less cumbersome than applying those rules in the server side language itself.

If you're simply using XSLT for formatting static content; it may not be the right tool. If it is dynamic content; why do you mind you have to transform ever time?

Also, avoid for-each. e.g.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>newstylesheet.xsl</title>
            </head>
            <body>
                <h2>My CD Collection</h2>
				<xsl:apply-templates select="catalog" />
            </body>
        </html>
    </xsl:template>
	
	<xsl:template match="catalog">
		<table border="1">
			<tr bgcolor="#9acd32">
				<th>Title</th><th>Artist</th>
			</tr>
			<xsl:apply-templates select="cd" />
		</table>
	</xsl:template>

	<xsl:template match="cd">
		<tr>
			<td><xsl:value-of select="title"/></td>
			<td><xsl:value-of select="artist"/></td>
		</tr>
	</xsl:template>

</xsl:stylesheet>


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1