Well I finally got this to work, thanks to all your help, some help from
another forum, and the input of a code-guru coworker. Here's the solution I
-we- came up with:
Firstly Two XSL sheets are better than one, this way its not necessary to run
the XML conversion if your data source is already properly formatted.
So if this is our XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//USER">
If you see <xsl:value-of select="."/> tell him this is the old tree.<BR/>
</xsl:for-each>
<xsl:for-each select="//AGENT/AGENTNAME">
If you see <xsl:value-of select="."/> tell him this is the new tree.<BR/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
it references the following tree structure:
<?xml version="1.0"?>
<DOCUMENT>
<AGENT>
<AGENTNAME>Agent Smith</AGENTNAME>
<AGENTID>Asmith</AGENTID>
<AGENTAGE>33</AGENTAGE>
<AGENTSTATUS>Agent</AGENTSTATUS>
</AGENT>
<AGENT>
<AGENTNAME>Agent Clyde</AGENTNAME>
<AGENTID>Aclyde</AGENTID>
<AGENTAGE>35</AGENTAGE>
<AGENTSTATUS>Agent in Training</AGENTSTATUS>
</AGENT>
</DOCUMENT>
Unfortunately sometimes the xml data looks like this:
<?xml version="1.0"?>
<DOCUMENT>
<USER>Agent Smith</USER>
<CLIENTDATA>Asmith</CLIENTDATA>
<CLIENTDATA>33</CLIENTDATA>
<CLIENTDATA>Agent</CLIENTDATA>
<USER>Agent Clyde</USER>
<CLIENTDATA>Aclyde</CLIENTDATA>
<CLIENTDATA>35</CLIENTDATA>
<CLIENTDATA>Agent in Training</CLIENTDATA>
</DOCUMENT>
So we need to convert the data using another stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:output indent="yes"/>
<xsl:key name="data-by-agent" match="CLIENTDATA"
use="generate-id(preceding-sibling::USER[1])"/>
<xsl:template match="/">
<DOCUMENT>
<xsl:apply-templates select="//USER" />
</DOCUMENT>
</xsl:template>
<xsl:template match="CLIENTDATA"/>
<xsl:template match="USER">
<AGENT>
<xsl:variable name="thisagentdata"
select="key('data-by-agent', generate-id())"/>
<AGENTNAME><xsl:value-of select="."/></AGENTNAME>
<AGENTID>
<xsl:value-of select="$thisagentdata[1]"/>
</AGENTID>
<AGENTAGE>
<xsl:value-of select="$thisagentdata[2]"/>
</AGENTAGE>
<AGENTSTATUS>
<xsl:value-of select="$thisagentdata[3]"/>
</AGENTSTATUS>
</AGENT>
</xsl:template>
</xsl:stylesheet>
And now let's put it all together, using an asp document that evaluates for a
need to convert or not, then performs conversion and outputs to the final
display:
<%
'fake variable for evaluation of xmldata source
dim oldXMLUser
oldXMLUSER = true
'create newSource variable
set newSource = server.createObject("Msxml2.DOMDocument")
newSource.async = False
newSource.validateOnParse = True
'load display stylesheet
set displayer = server.createObject("Msxml2.DOMDocument")
displayer.async = false
displayerfile = Server.mappath("postconversion.xsl")
displayer.load displayerfile
'evaluate for conversion
if oldXMLUser then
'load the data
set source = server.createObject("Msxml2.DOMDocument")
source.async = false
dataFile = Server.mappath("convertxml.xml")
source.load dataFile
'convert the data
set xmlconverter = server.createObject("Msxml2.DOMDocument")
xmlconverter.async = false
xmlconverterfile = Server.mappath("preconversion.xsl")
xmlconverter.load xmlconverterfile
'define newSource
source.transformNodeToObject xmlconverter, newSource
else
'load the data
set source = server.createObject("Msxml2.DOMDocument")
source.async = false
goodFile = Server.mappath("goodformat.xml")
newSource.load goodFile
end if
'display final page
response.write newSource.transformNode(displayer)
%>
Phew! It works... yippy.
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list