xsl-list
[Top] [All Lists]

[xsl] Help with xsl:result-document

2010-10-25 16:12:00

I've come to an impasse with trying to use xsl:result-document.  I have a 
number of XML log files that I'm trying to extract data from and place that 
data into a database.  I think it best to have 4 tables - OutCommand, 
InResponse, InCommand and OutResponse.  To ease my SQL bulk import coding, I 
thought to produce 4 output files from one XSLT2.0 transform.  However, I just 
can't figure out how to get result-document to work with my obvious limited 
knowledge of transforms.  I'm looking to get 4 output files - 
OutCommandoutput.xml, InResponse-output.xml, InCommand-output.xml and 
OutResponse-output.xml.  But I can't use templates, as I've more than one 
'match' - and so the template tries (and obviously fails) to create another 
output stream to something it's already created.  But I can't see a way out of 
this, so any advice, pointers or help will be, as ever, appreciated.

Neil

XML:-

<MyLog Device="Gateway.server" Time="25/10/2010 08:57:40">
<OutCommand Command="SendData" Time="25/10/2010 08:57:40" Port="1024" 
IP="10.10.10.1" LinkID="D1D83BE0-8138-47C2-99A3DF5809A5DA63">
</OutCommand>
<InResponse Command="Ack" Time="25/10/2010 08:57:40" Port="1024" 
IP="10.10.10.2" LinkID="D1D83BE0-8138-47C2-99A3DF5809A5DA63">
</InResponse>
<InCommand Command="heartbeat" Time="25/10/2010 08:58:10" Port="1025" 
IP="10.10.10.2">
</InCommand>
<OutResponse Command="heartbeat" Time="25/10/2010 08:58:10" Port="1025" 
IP="10.10.10.1">
</OutResponse>
<OutCommand Command="SendData" Time="25/10/2010 08:58:40" Port="1024" 
IP="10.10.10.1" LinkID="D1D83BE0-8138-47C2-99A3DF5809A5DA64">
</OutCommand>
<InResponse Command="Ack" Time="25/10/2010 08:58:40" Port="1024" 
IP="10.10.10.2" LinkID="D1D83BE0-8138-47C2-99A3DF5809A5DA64">
</InResponse>
<InCommand Command="heartbeat" Time="25/10/2010 08:59:10" Port="1025" 
IP="10.10.10.2">
</InCommand>
<OutResponse Command="heartbeat" Time="25/10/2010 08:59:10" Port="1025" 
IP="10.10.10.1">
</OutResponse>
</MyLog>



XSLT:-

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="MyLog">
    <xsl:element name="Device">
        <xsl:apply-templates select="OutCommand"/>
        <xsl:apply-templates select="InResponse"/>
        <xsl:apply-templates select="InCommand"/>
        <xsl:apply-templates select="OutResponse"/>
    </xsl:element>
</xsl:template>

<xsl:template match="OutCommand">
<xsl:result-document method="xml" href="OutCommandoutput.xml">
    <OutCommand>
        <xsl:element name="Command"><xsl:apply-templates 
select="@Command"/></xsl:element>
         <xsl:for-each select="@Time">
             <Time>
              <xsl:value-of 
select="concat(substring(.,7,4),'-',substring(.,4,2),'-',substring(.,1,2),'T',substring(.,12,8)
 )"/>
              </Time>
         </xsl:for-each>
        <xsl:element name="Port"><xsl:apply-templates 
select="@Port"/></xsl:element>
        <xsl:element name="LinkID"><xsl:apply-templates 
select="@LinkID"/></xsl:element>
    </OutCommand>
</xsl:result-document>
</xsl:template>

<xsl:template match="InResponse">
<xsl:result-document method="xml" href="InResponse-output.xml">
    <InResponse>
        <xsl:element name="Command"><xsl:apply-templates 
select="@Command"/></xsl:element>
         <xsl:for-each select="@Time">
             <Time>
              <xsl:value-of 
select="concat(substring(.,7,4),'-',substring(.,4,2),'-',substring(.,1,2),'T',substring(.,12,8)
 )"/>
              </Time>
         </xsl:for-each>
        <xsl:element name="Port"><xsl:apply-templates 
select="@Port"/></xsl:element>
        <xsl:element name="LinkID"><xsl:apply-templates 
select="@LinkID"/></xsl:element>
    </InResponse>
</xsl:result-document>
</xsl:template>

<xsl:template match="InCommand">
<xsl:result-document method="xml" href="InCommand-output.xml">
    <InCommand>
        <xsl:element name="Command"><xsl:apply-templates 
select="@Command"/></xsl:element>
         <xsl:for-each select="@Time">
             <Time>
              <xsl:value-of 
select="concat(substring(.,7,4),'-',substring(.,4,2),'-',substring(.,1,2),'T',substring(.,12,8)
 )"/>
              </Time>
         </xsl:for-each>
        <xsl:element name="Port"><xsl:apply-templates 
select="@Port"/></xsl:element>
        <xsl:element name="LinkID"><xsl:apply-templates 
select="@LinkID"/></xsl:element>
    </InCommand>
</xsl:result-document>
</xsl:template>

<xsl:template match="OutResponse">
<xsl:result-document method="xml" href="OutResponse-output.xml">
    <OutResponse>
        <xsl:element name="Command"><xsl:apply-templates 
select="@Command"/></xsl:element>
         <xsl:for-each select="@Time">
             <Time>
              <xsl:value-of 
select="concat(substring(.,7,4),'-',substring(.,4,2),'-',substring(.,1,2),'T',substring(.,12,8)
 )"/>
              </Time>
         </xsl:for-each>
        <xsl:element name="Port"><xsl:apply-templates 
select="@Port"/></xsl:element>
        <xsl:element name="LinkID"><xsl:apply-templates 
select="@LinkID"/></xsl:element>
    </OutResponse>
    </xsl:result-document>
</xsl:template>

</xsl:stylesheet>

                                          
--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--