xsl-list
[Top] [All Lists]

Real-time XML to XML Conversion?

2003-06-10 09:19:39
I've been charged with the dubious task of creating an XSLT that will convert 
XML to XML live. I'm almost there, but I need some help.

First things first... I need to convert this:

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

Into this:

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

But I have a problem... I'm getting this:

<DOCUMENT>
<AGENT>
<AGENTNAME>Agent Smith</AGENTNAME>
<AGENTID>Asmith</AGENTID>
<AGENTAGE>33</AGENTAGE>

<AGENTSTATUS>Agent</AGENTSTATUS>Agent Clyde</AGENT>

<AGENT>
<AGENTNAME>Agent Clyde</AGENTNAME>
<AGENTID>Aclyde</AGENTID>
<AGENTAGE>35</AGENTAGE>
<AGENTSTATUS>Agent in Training</AGENTSTATUS>
</AGENT>
</DOCUMENT>

Notice I've got an extra "Agent Clyde" where I should have just the closing 
</AGENT>.

My XSL looks like this:

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

</xsl:template>
</xsl:stylesheet>


My second question is one of functionality. While what I've tried so far does 
produce an output file that looks like a new XML file, that's not what I need. 
I actually need the new XML data to be immediately available to be manipulated 
by the XSLT which is doing the tree conversion. Make sense?

Oh and finally I'm using an .asp to transform the xsl/xml as follows:

dataFile = Server.mappath("convertxml.xml") 
set XMLDoc = server.createObject("Msxml2.DOMDocument") 
XMLDoc.async = false 
XMLDoc.load dataFile 

set XSLDoc = server.createObject("Msxml2.DOMDocument") 
XSLDoc.async = false 
datafile=server.mappath("convertxml.xsl") 
XSLDoc.load datafile response.write 

XMLDoc.transformNode(XSLDoc) 

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



<Prev in Thread] Current Thread [Next in Thread>