Using XML XSL transformation in depth

Using XML XSL transformation in depth

It is very convenient to transform an XML source file to a HTML page using XSL style sheet via JSTL x: tag.

<x:transform doc="${XML}" xslt="${XSL}">

 

However, in some occasion, you might want to pass some dynamic parameters into the HTML page you are about to generate, for example, the current date.

<x:transform doc="${XML}" xslt="${XSL}">

                <x:param name="currentDate" value="${currentDate}"/>   

</x:transform>

 

The preceding code will help you to inject the parameters into the transformation. You can define how to use this parameter in your XSL file

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

    <xsl:output method="html" omit-xml-declaration="yes" encoding="ISO-8859-1" indent="yes"/>

<xsl:param name="currentDate"/>

<xsl:template match="/">

         <xsl:value-of select="$currentDate"/>

</xsl:stylesheet>

 

How about when you want to use the parameter in a hyperlink?

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

    <xsl:output method="html" omit-xml-declaration="yes" encoding="ISO-8859-1" indent="yes"/>

<xsl:param name="currentDate"/>

<xsl:template match="/">

                    <xsl:attribute name="href">http://hongliang.com/

                        <xsl:value-of select="$currentDate"/>.html              

                    </xsl:attribute>

</xsl:stylesheet>

 

Let’s make it more complex. How about you need to get the XML source from a remote GZIP file? We would need to use Apache HttpClient for downloading and java.util.zip for convert GZIP file into an input stream of the XML source.

        try {

            // read file from filesystem using http client, then unzip the gzip to inputstream

            HttpMethod getMethod = new GetMethod("http://hongliang.com&quot;);

            getMethod.setQueryString("?a=dongo&filePath=" + filePath);

 

            int status = new HttpClient().executeMethod(getMethod);

            if (status == HttpStatus.SC_OK) {

                InputStream inputStream = getMethod.getResponseBodyAsStream();

                return new ByteArrayInputStream(FileCopyUtils.copyToByteArray(new GZIPInputStream(inputStream)));

            }

        } catch (IOException e) {

            log.error("Failed to ungzip file to server", e);

        }

 

FileCopyUtils.copyToByteArray() from org.springframework.util is used create a copy of the XML source input stream into a new byte array. Since we are using this XML byte input stream as transformation source, we need to convert this it into StreamSource (which implements Source). If your application use Spring MVC, you probably can use the following.

 

ModelAndView mav = new ModelAndView(getViewName());

mav.addObject("XML", new StreamSource(xml));

 

 

Advertisements
This entry was posted in XML. Bookmark the permalink.

2 Responses to Using XML XSL transformation in depth

  1. GFF says:

    您的部落格文章真棒!!有空我一定會常來逛!!—-幫忙踩個人氣 http://feg8.com.tw

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s