[xsl] XSLT real time use of XML to XML Transformation (Updated Querry)

Subject: [xsl] XSLT real time use of XML to XML Transformation (Updated Querry)
From: "HEATH ALLISON" <HEATH.ALLISON@xxxxxxxxxxxxxxx>
Date: Tue, 10 Jun 2003 15:29:47 -0500
I've made some progress hacking away at my problem using information I found in the archives, but I'm still stuck on a few points. First let me recap what I am trying to do:

I have a single XSL file that I need to parse XML data from two sources who are not giving me identicle node-trees. The following dummy code represents what one source of data would look like:

XML File #1:
<?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>

My XSL sheet prefers a structure like this:
<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>

This is what I'm getting as a result (the word "stop!" has been included as part of my debugging exercises. I'm trying to track down what is causing an irregularity in the formation of the XML document.

<DOCUMENT>
	<AGENT>
		<AGENTNAME>Agent Smith</AGENTNAME>
		<AGENTID>Asmith</AGENTID>
		Stop!
		<AGENTAGE>33</AGENTAGE>
		Stop!
		<AGENTSTATUS>Agent</AGENTSTATUS>
		Stop!
		Agent Clyde
		Stop!
		Stop!
		Stop!
	</AGENT>
	<AGENT>
		<AGENTNAME>Agent Clyde</AGENTNAME>
		<AGENTID>Aclyde</AGENTID>
		Stop!
		<AGENTAGE>35</AGENTAGE>
		Stop!
		<AGENTSTATUS>Agent in Training</AGENTSTATUS>
		Stop!
	</AGENT>
</DOCUMENT>

Here is my ASP file that attempts to parse the errant XML file, convert it to a usable tree, and then parse the same XSL file with the new DOM object.

ASP File
<%
'load the data
set source = server.createObject("Msxml2.DOMDocument")
source.async = false
dataFile = Server.mappath("convertxml.xml")
source.load dataFile

'load stylesheet
set XSLDoc = server.createObject("Msxml2.DOMDocument")
XSLDoc.async = false
stylesheetfile=server.mappath("convertxml.xsl")
XSLDoc.load stylesheetfile

Response.Write "<B>everthing after this line is the first transformation <BR></B>"
response.write source.transformNode(XSLDoc)
Response.Write "<B>everthing before this line is the first transformation <BR></B>"


'maker do
set newSource = server.createObject("Msxml2.DOMDocument")
newSource.async = False
newSource.validateOnParse = True
source.transformNodeToObject XSLDoc,newSource

Response.Write "<B>everthing after this line is the second transformation</B><BR>"
response.write newSource.transformNode(XSLDoc)
Response.Write "<B>everthing before this line is the second transformation</B><BR>"
%>

Finally here's my XSL File. The "Stop!" as mentioned is what I'm using to try and debug a problem in the output formation of my transformed XML. What I've concluded from this is that the XSL is not stopping when it runs out of CLIENTDATA nodes, and leaving blank spaces for each node, and consequently breaking my XML. I've tried some things to fix this, but nothing has worked yet. I don't know if fixing that problem will fix my second problem which is, that the second transformation isn't working. See the end of this document to see an example of the output of the page as it currently exists.

<?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="/">
<DOCUMENT>
	<xsl:for-each select="//USER">
	<AGENT>
		<AGENTNAME><xsl:value-of select="."/></AGENTNAME>
        <xsl:apply-templates select="following-sibling::*"/>
	</AGENT>
	</xsl:for-each>
</DOCUMENT>

<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:for-each select="//USER">
If you see <xsl:value-of select="."/> tell him this is the old tree.<BR/>
</xsl:for-each>

</xsl:template>

<xsl:template match="CLIENTDATA">

	<xsl:if test="position()=1">
		<AGENTID><xsl:value-of select="." /></AGENTID>
	</xsl:if>
	<xsl:if test="position()=2">
		<AGENTAGE><xsl:value-of select="." /></AGENTAGE>
	</xsl:if>
	<xsl:if test="position()=3">
		<AGENTSTATUS><xsl:value-of select="." /></AGENTSTATUS>
	</xsl:if>

	Stop!
</xsl:template>


</xsl:stylesheet>


Finally, this is what happens when I run my .asp as it currently exists:

<B>everthing after this line is the first transformation <BR></B><DOCUMENT>
<AGENT>
<AGENTNAME>Agent Smith</AGENTNAME>
<AGENTID>Asmith</AGENTID>
	Stop!
<AGENTAGE>33</AGENTAGE>
	Stop!
<AGENTSTATUS>Agent</AGENTSTATUS>
	Stop!
Agent Clyde
	Stop!
	Stop!
	Stop!
</AGENT>
<AGENT>
<AGENTNAME>Agent Clyde</AGENTNAME>
<AGENTID>Aclyde</AGENTID>
	Stop!
<AGENTAGE>35</AGENTAGE>
	Stop!
<AGENTSTATUS>Agent in Training</AGENTSTATUS>
	Stop!
</AGENT>
</DOCUMENT>
If you see Agent Smith tell him this is the old tree.<BR />
If you see Agent Clyde tell him this is the old tree.<BR />
<B>everthing before this line is the first transformation <BR></B>

<B>everthing after this line is the second transformation</B><BR>
<DOCUMENT>
</DOCUMENT>
<B>everthing before this line is the second transformation</B><BR>

If anyone could shed some light on this I'd be grateful... so would the wall I've been beating my head against.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread