Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 3rd, 2010, 10:52 PM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Automating XML transformation

I apologize in advance if this is an amateurish question, but I am having no luck figuring out how to simplify/automate the following workflow. Here is what I need to do:

- Save multiple XML files from the Web and save each as a specific filename
- Apply my XSLT stylesheet to each one - resaving the file in its new, transformed format

I am able to do this by manually accessing each file through a browser, saving it to the filename I desire, and then manually editing each file - adding the reference line to my XSLT stylesheet within each one.

This manual process works for me, but is not ideal as it takes quite some time and needs to be done more than once per day.

What is the best way to automate what I described above? Any help here is greatly appreciated. Thanks!
 
Old June 4th, 2010, 02:45 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you are using XSLT, and the URLs and the filenames do not change then you can use the document() function, and the xsl:result-document instruction to perform all the downloads and transformations within your stylesheet:

Code:
<xsl:result-document href="output1.xml">
  <xsl:apply-templates select="document('http://www.test.com/download1.xml')/element"/>
</xsl:result-document>
Otherwise you are likely looking at writing something in a language other than XSLT, in which case this forum isn't the best place to ask, and the answer will depend on what you know (i.e. if you know perl, write it in perl - if you know java, write it in java etc).
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 4th, 2010, 04:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Another way of doing it would be to write a little shell script or command file containing a sequence of calls to your XSLT processor, for example:

java net.sf.saxon.Transform -s:http://one.site/document1.xml -xsl:my.xsl -o:document1.html

java net.sf.saxon.Transform -s:http://two.site/document2.xml -xsl:my.xsl -o:document2.html

(However, because this involves restarting the Java VM for each transformation, putting the logic within a single stylesheet and using the document() function is likely to be faster).

Using the <?xml-stylesheet?> processing instruction isn't a good way to do this. All XSLT processors have some kind of interface that allows you to run a given stylesheet against a given source document.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 4th, 2010, 09:50 AM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
If you are using XSLT, and the URLs and the filenames do not change then you can use the document() function, and the xsl:result-document instruction to perform all the downloads and transformations within your stylesheet:

Code:

Code:
<xsl:result-document href="output1.xml">
  <xsl:apply-templates select="document('http://www.test.com/download1.xml')/element"/>
</xsl:result-document>
This seems like an ideal solution. The issue I'm running into when using this is that the XML files I am accessing have parameters at the end of the URL. Example: http://www.domain.com/xml/statistics_en.xml?A=420&ST=RT

When adding the following lines to my stylesheet, I receive this error message - "XML Parsing Error: not well-formed" pointing at line #5, which is the line below containing the XML URL:

Code:
<xsl:result-document href="output1.xml">
  <xsl:apply-templates select="document('http://www.domain.com/xml/statistics_en.xml?A=420&ST=RT')/element"/>
</xsl:result-document>

Could these parameters at the tail end of the source URL cause this error? Please note that when I save the XML locally and then run my stylesheet against it, the output is perfect - so I think that the original XML is not poorly formed in this case.

Thanks again...
 
Old June 4th, 2010, 09:57 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

An XSLT stylesheet needs to be well-formed XML document so you need to escape any ampersand not starting an entity or character reference as
Code:
&amp;
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 4th, 2010, 10:17 AM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin. That fixed that problem. However, when opening my stylesheet in a browser, I am getting the following error now:
Quote:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
Here is my stylesheet in its entirety. Remember that this stylesheet works when applied manually to this XML when it is saved locally...

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:result-document href="output1.xml">
  <xsl:apply-templates select="document('http://www.domain.com/xml/statistics_en.xml?A=420&amp;ST=RT')/element"/>
</xsl:result-document>

<xsl:template match="/">
  <html>
  <body>
  <h2>Packages</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Price</th>
      </tr>
      <xsl:for-each select="products/product">
      <tr>
        <td><xsl:value-of select="product_name"/></td>
        <td><xsl:value-of select="price"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
 
Old June 4th, 2010, 10:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You might hit problems running this in a browser because of security issues. However, if you want to try it, the simplest way is not to load the stylesheet in the browser, but to load a dummy XML document that references the stylesheet using an <?xml-stylesheet?> processing instruction.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 4th, 2010, 11:56 AM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
You might hit problems running this in a browser because of security issues. However, if you want to try it, the simplest way is not to load the stylesheet in the browser, but to load a dummy XML document that references the stylesheet using an <?xml-stylesheet?> processing instruction.
Thanks. I guess I'm having trouble grasping the concept of xsl:result-document. How do I force this result document to be produced?
 
Old June 4th, 2010, 12:01 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

xsl:result-document is an XSLT 2.0 instruction and as yet there is no XSLT 2.0 processor that runs in the browser, so it's not clear how browsers will interpret it. When running in other environments, e.g. from the command line, an XSLT 2.0 stylesheet that uses xsl:result-document will typically write multiple output files to local filestore.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 4th, 2010, 02:33 PM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
xsl:result-document is an XSLT 2.0 instruction and as yet there is no XSLT 2.0 processor that runs in the browser, so it's not clear how browsers will interpret it. When running in other environments, e.g. from the command line, an XSLT 2.0 stylesheet that uses xsl:result-document will typically write multiple output files to local filestore.
Do you recommend a particular command line interpreter to accomplish what I'm trying to do here?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding xml-html transformation of an xml string using xslt and javascript suprakash444 XSLT 1 January 12th, 2009 01:23 AM
xml transformation spring152103 XSLT 9 October 14th, 2008 07:50 PM
Is XML supports transformation of HTML to XML? zeeonline XSLT 1 July 28th, 2006 05:13 PM
Xml to Xml Transformation using xslt ShaileshShinde XSLT 1 July 20th, 2005 01:20 AM
XML to XML transformation using XSLT karjagis XSLT 3 July 30th, 2004 06:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.