Subject: RE: RE: [xsl] RE: Transformation XML to XML From: cknell@xxxxxxxxxx Date: Tue, 19 Jul 2005 10:58:13 -0400 |
This is really a lot simpler than you imagine. The hardest thing for traditional programmers to shake is the idea of controlling everything that's going on. XSLT is more like SQL than Java or VB. You describe what you want and let the XSLT engine worry about the details. You didn't give a clue as to what the root element is in your source file, so I enclosed your two records in a "records" element like this: <?xml version='1.0'?> <records> <image> <pdesc>hot rod</pdesc> <filename>ab_0001.jpg</filename> <photoid>223</photoid> <photogid>5</photogid> </image> <image> <pdesc>brass headlamps</pdesc> <filename>ab_0002.jpg</filename> <photoid>224</photoid> <photogid>5</photogid> </image> </records> This stylesheet gives the output you showed: <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" encoding="UTF-8" /> <xsl:strip-space elements="*" /> <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="records"> <collection> <xsl:apply-templates /> </collection> </xsl:template> <xsl:template match="image"> <image pdesc="{pdesc}" filename="{filename}" photoid="{photoid}" photogid="{photogid}" /> </xsl:template> </xsl:stylesheet> Write back with any questions. -- Charles Knell cknell@xxxxxxxxxx - email -----Original Message----- From: Rob Merrison <rob.merrison@xxxxxxxxxxxx> Sent: Tue, 19 Jul 2005 15:39:05 +0100 To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Subject: RE: [xsl] RE: Transformation XML to XML Hi, Thanks for the reply and your not kidding, so your help here would be great. Anyway, here goes, The XML is produced as a result of an Access Database query that is shown in the ASP (VBScript) file in my previous message. The table field names are used to create the XML tags. So in answer to your question is no actual XML file, its done on the fly. The following is what I currently get, but all in one long string. However, it's missing the root, which I need to add as part of the stylesheet. This is a sample of 2 image records; there will be hundreds of image records in the database. <image> <pdesc>hot rod</pdesc> <filename>ab_0001.jpg</filename> <photoid>223</photoid> <photogid>5</photogid> </image> <image> <pdesc>brass headlamps</pdesc> <filename>ab_0002.jpg</filename> <photoid>224</photoid> <photogid>5</photogid> </image> What I'm actually after is the following: converting the pdesc, filename, photoid and photogid as attributes of image with collection as the root. Eg. <collection> <image pdesc="hot rod" filename="ab_0001.jpg" photoid="223" photogid="5"/> <image pdesc="brass headlamps" filename="ab_0002.jpg" photoid="224" photogid="5"/> </collection> I hope this makes sense. I'm using Flash to read this and I'm told that it can read large amounts of XML a lot quicker if it is presented with attributes hence the reason for this. Cheers Rob. -----Original Message----- From: cknell@xxxxxxxxxx [mailto:cknell@xxxxxxxxxx] Sent: 19 July 2005 15:12 To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: RE: [xsl] RE: Transformation XML to XML All the "disable-output-escaping" tells me you are struggling with the idea of XSLT. Please give a sample of the input file and we can put you on the right track. -- Charles Knell cknell@xxxxxxxxxx - email -----Original Message----- From: Rob Merrison <rob.merrison@xxxxxxxxxxxx> Sent: Tue, 19 Jul 2005 10:31:58 +0100 To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Subject: [xsl] RE: Transformation XML to XML Hi I need some help please with the following xslt stylesheet. I use this style sheet that gets pulled into the ASP file after the query is run on an ACCESS database. The output is xml. However the xml is not formed to the correct layout. This file is called ADOGeneric.xsl and is referenced in the ASP file. <?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:z="#RowsetSchema"> <s:Schema id="RowsetSchema"/> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:template match="/"> <xsl:apply-templates select="//z:row"/> </xsl:template> <xsl:template match="z:row"> <xsl:text disable-output-escaping="no"><image></xsl:text> <xsl:for-each select="@*"> <xsl:text disable-output-escaping="no"><</xsl:text> <xsl:value-of select="name()"/> <xsl:text disable-output-escaping="no">></xsl:text> <xsl:value-of select="."/> <xsl:text disable-output-escaping="no"></</xsl:text> <xsl:value-of select="name()"/> <xsl:text disable-output-escaping="no">></xsl:text> </xsl:for-each> <xsl:text disable-output-escaping="no"></image></xsl:text> </xsl:template> </xsl:stylesheet> <%@ Language = VBScript %> <% Response.Expires = -1 Response.Buffer = True Dim conn, rs, xml, xsl Set conn = Server.CreateObject("ADODB.Connection") conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("gen_dib_data.mdb") & ";User Id=admin;Password=;" conn.Open Set rs = Server.CreateObject("ADODB.Recordset") Set rs.ActiveConnection = conn rs.Open "SELECT Photo.PDesc, Photo.Filename, Photo.PhotoID, Photo.PhotogID FROM Photo" alldata=rs.getrows() numcols=ubound(alldata,1) numrows=ubound(alldata,2) Set xml = Server.CreateObject("MSXML2.DOMDocument") Set xsl = Server.CreateObject("MSXML2.DOMDocument") xml.async = False xsl.async = False rs.Save xml, 1 'adPersistXML xsl.load Server.MapPath("ADOGeneric.xsl") Response.Write(lcase(xml.transformNode(xsl))) Set xsl = Nothing Set xml = Nothing rs.Close Set rs = Nothing conn.Close Set conn = Nothing %> The output currently looks like this, one continuous line: <image><pdesc>hot rod</pdesc><filename>ab_0001.jpg</filename><photoid>223</photoid><photogid>5 </photogid></image><image><pdesc>brass headlamps</pdesc><filename>ab_0002.jpg</filename><photoid>224</photoid><phot ogid>5</photogid></image> which I guess if I split it up it would look like this: <image> <pdesc>hot rod</pdesc> <filename>ab_0001.jpg</filename> <photoid>223</photoid> <photogid>5</photogid> </image> <image> <pdesc>brass headlamps</pdesc> <filename>ab_0002.jpg</filename> <photoid>224</photoid> <photogid>5</photogid> </image> I actually need an xslt script to produce the output to look like the following if this is possible with the added element collection: <collection> <image pdesc="hot rod" filename="ab_0001.jpg" photoid="223" photogid="5"/> <image pdesc="brass headlamps" filename="ab_0002.jpg" photoid="224" photogid="5"/> </collection> I tried this in order to achieve the results but this fails with the message below and I'm pretty sure this is almost correct. <?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:element name="collection"> <xsl:for-each select="/collection/image" > <xsl:element name='{name()}'> <xsl:for-each select="*" > <xsl:attribute name='{name()}'> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> </xsl:element> </xsl:for-each> </xsl:element> </xsl:template> </xsl:stylesheet> The XML page cannot be displayed Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later. ---------------------------------------------------------------------------- ---- Switch from current encoding to specified encoding not supported. Error processing resource 'http://thebes/ImageRealm_new_1... <?xml version="1.0" encoding="utf-16"?><collection></collection> ---------------------------------------^ many thanks Rob.
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] RE: Transformation XML to, David Carlisle | Thread | RE: [xsl] RE: Transformation XML to, Rob Merrison |
RE: [xsl] RE: Transformation XML to, Michael Kay | Date | Re: [xsl] RE: Transformation XML to, David Carlisle |
Month |