 |
| 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
|
|
|
|

December 17th, 2004, 08:47 AM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Transform XML to XML
Hi,
I would like to execute a sorting on a XML file and then save it to the file i was reading from.
The Input XML:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<publications>
<publication SourceName="Plumtree" SourceUNID="278">
<title>R&K Updates</title>
<category>R & K News</category>
<security users="2416" groups="1,51,200"/>
</publication>
(...)
<publications>
The Output XML should be sorted by
category
title
Thank you for any suggestions!!!
|
|

December 17th, 2004, 09:04 AM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
so, I've created a .xsl and an .asp file:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:param name="sortByTitle" select="'title'"/>
<xsl:param name="sortByCat" select="'category'"/>
<xsl:param name="strXPath" select="//publication"/>
<xsl:template match="/">
<publications>
<xsl:apply-templates select="$strXPath">
<xsl:sort select="*[name()=$sortByCat]" order="ascending"/>
<xsl:sort select="*[name()=$sortByTitle]" order="ascending"/>
</xsl:apply-templates>
</publications>
</xsl:template>
<xsl:template match="publication">
<tr>
<td><xsl:value-of select="category"/></td>
<td><xsl:value-of select="title"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Code:
<%@ Language=JScript%>
<%Response.Buffer=false%>
<%
var objXMLDoc = Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
objXMLDoc.async = false;
objXMLDoc.load(Server.MapPath("index.xml"));
// Build the XPath Query
var xmlQuery = "//publications/publication";
// Create a nodeset for the selected XPath
var objNodes = objXMLDoc.selectNodes(xmlQuery);
// Create an instance of the xslt object
var xsl=Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
xsl.async = false;
xsl.load(Server.MapPath("sortByCategoryThenByTitle.xsl"));
// Create an instance of the template object to add the parameter
var xslt = Server.CreateObject("Msxml2.XSLTemplate");
xslt.stylesheet = xsl;
xslProc = xslt.createProcessor();
xslProc.input = objXMLDoc;
xslProc.addParameter("sortByTitle", "title"); //Passing in the Sort Criteria
xslProc.addParameter("sortByCat", "category"); //Passing in the Sort Criteria
xslProc.addParameter("strXPath", objNodes); //Passing in a Nodeset
//Apply the Transformation and save the result
xslProc.transform();
xslProc.save(Server.MapPath("index3.xml"));
%>
Why o Why???
|
|

December 17th, 2004, 09:21 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
start with an identity transformation, then add a specific template to sort the publication elements:
Code:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="publications">
<xsl:copy>
<xsl:apply-templates select="publication">
<xsl:sort select="category"/>
<xsl:sort select="title"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
|
|

December 17th, 2004, 09:36 AM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
pgtips thanks a lot! You're the first who replies to a post of mine ;)
Your suggestion implies that i do not have to add paramaters in the vb script:
Code:
<%@ Language=JScript%>
<%Response.Buffer=false%>
<%
var objXMLDoc = Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
objXMLDoc.async = false;
objXMLDoc.load(Server.MapPath("index.xml"));
// Build the XPath Query
var xmlQuery = "//publications/publication";
// Create a nodeset for the selected XPath
var objNodes = objXMLDoc.selectNodes(xmlQuery);
// Create an instance of the xslt object
var xsl=Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
xsl.async = false;
xsl.load(Server.MapPath("sortByCategoryThenByTitle.xsl"));
// Create an instance of the template object to add the parameter
var xslt = Server.CreateObject("Msxml2.XSLTemplate");
xslt.stylesheet = xsl;
xslProc = xslt.createProcessor();
xslProc.input = objXMLDoc;
//Apply the Transformation and save the result
xslProc.transform();
xslProc.save(Server.MapPath("index3.xml"));
%>
I believe that I have a bug in this code, could you have a look at it?
|
|

December 17th, 2004, 09:56 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
1. looks like you haven't declared xslProc
2. there is no save method on IXSLProcessor interface, use output property instead to output directly to a new DOM object, then use the save method on the new DOM
3. you're mixing progids from different msxml versions, try these instead:
Msxml2.FreeThreadedDOMDocument.4.0
Msxml2.XSLTemplate.4.0
(use .3.0 instead if you have MSXML3 but not MSXML4)
hth
Phil
|
|

December 17th, 2004, 10:33 AM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, that I posted such obvious bugs... Afterwards it's mostly clear :-)
I simplified the whole thing but the output from the processor to a DOM does not work as i expect, maybe you've got a hint in your pocket?
Code:
<% @Language = "VBScript" %>
<%
Set XSLTMP = Server.CreateObject("Msxml2.XSLTemplate.4.0")
Set XSLDOK = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.4.0")
Set XMLOUT = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.4.0")
XSLDOK.Async = false
XSLDOK.Load(Server.MapPath("sortByCategoryThenByTitle.xsl"))
XSLTMP.Stylesheet = XSLDOK
Set XMLDOK = Server.CreateObject("Msxml2.DOMDocument.4.0")
XMLDOK.Async = false
XMLDOK.Load(Server.MapPath("index.xml"))
Set XSLPU = XSLTMP.CreateProcessor()
XSLPU.Input = XMLDOK
XSLPU.Transform()
XSLPU.output (XMLOUT)
XMLOUT.save (Server.MapPath("index3.xml"))
%>
|
|

December 17th, 2004, 10:56 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
you have to set the output property BEFORE you call the Transform method, also as its a property you use XSLPU.output = XMLOUT
|
|

December 17th, 2004, 11:01 AM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Great, it works
Lessons learned:
1) A property is e.g. a behaviour and has to be specified before using it...
2)XSL is powerfull
3)check for current progids
4)Phil is a nice guy
So, let's enjoy the weekend
|
|

December 17th, 2004, 11:28 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Great, glad it works for you now. Yes bring on the weekend :D
|
|
 |