Help with a transform
Posted: Fri Jun 01, 2007 8:08 pm
Hello,
I have an XML from ADO recordset which looks like the sample below. I am transforming it to indent the z:rows based on ID and parent ID using the the XSL below.
I can nest the rows properly but I would like to have the final output look exactly like the original except the nested row.
I have a very limited knowledge of the XSLT, any help is appriciated. You can cut and paste the input XML and XSLT in oxygen to experiment
MY INPUT XML=======================================
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly">
<s:AttributeType name="ID" rs:number="1" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="ParentID" rs:number="2" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="Title" rs:number="4" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="string" dt:maxLength="100"/>
</s:AttributeType>
<s:extends type="rs:rowbase"/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row ID="1" ParentID="0" Title="Security"/>
<z:row ID="30" ParentID="1" Title="Very nice"/>
<z:row ID="109" ParentID="30" Title="Using Event mg"/>
<z:row ID="185" ParentID="21" Title="Sub project 1"/>
<z:row ID="186" ParentID="185" Title="Sub project 1.1"/>
<z:row ID="187" ParentID="21" Title="Sub project 2"/>
<z:row ID="188" ParentID="187" Title="Sub project 2.1"/>
<z:row ID="189" ParentID="21" Title="Sub project 3"/>
<z:row ID="193" ParentID="1" Title="Sub project 4"/>
<z:row ID="194" ParentID="21" Title="Sub project 5"/>
<z:row ID="195" ParentID="0" Title="Sub project 6"/>
</rs:data>
</xml>
MY XSLT TRANSFOR======================================
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema" exclude-result-prefixes="s dt rs z" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="/xml/rs:data/z:row[not(@ParentID = /xml/rs:data/z:row/@ID)]"/>
</xsl:template>
<xsl:template match="z:row">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="/xml/rs:data/z:row[@ParentID = current()/@ID]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
MY INTENDED OUTPUT======================================
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly">
<s:AttributeType name="ID" rs:number="1" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="ParentID" rs:number="2" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="Title" rs:number="4" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="string" dt:maxLength="100"/>
</s:AttributeType>
<s:extends type="rs:rowbase"/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row ID="1" ParentID="0" Title="Security">
<z:row ID="30" ParentID="1" Title="Very nice">
<z:row ID="109" ParentID="30" Title="Using Event mg"/>
</z:row>
<z:row ID="193" ParentID="1" Title="Sub project 4"/>
</z:row>
<z:row ID="185" ParentID="21" Title="Sub project 1">
<z:row ID="186" ParentID="185" Title="Sub project 1.1"/>
</z:row>
<z:row ID="187" ParentID="21" Title="Sub project 2">
<z:row ID="188" ParentID="187" Title="Sub project 2.1"/>
</z:row>
<z:row ID="189" ParentID="21" Title="Sub project 3"/>
<z:row ID="194" ParentID="21" Title="Sub project 5"/>
<z:row ID="195" ParentID="0" Title="Sub project 6"/>
</rs:data>
</xml>
I have an XML from ADO recordset which looks like the sample below. I am transforming it to indent the z:rows based on ID and parent ID using the the XSL below.
I can nest the rows properly but I would like to have the final output look exactly like the original except the nested row.
I have a very limited knowledge of the XSLT, any help is appriciated. You can cut and paste the input XML and XSLT in oxygen to experiment
MY INPUT XML=======================================
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly">
<s:AttributeType name="ID" rs:number="1" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="ParentID" rs:number="2" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="Title" rs:number="4" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="string" dt:maxLength="100"/>
</s:AttributeType>
<s:extends type="rs:rowbase"/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row ID="1" ParentID="0" Title="Security"/>
<z:row ID="30" ParentID="1" Title="Very nice"/>
<z:row ID="109" ParentID="30" Title="Using Event mg"/>
<z:row ID="185" ParentID="21" Title="Sub project 1"/>
<z:row ID="186" ParentID="185" Title="Sub project 1.1"/>
<z:row ID="187" ParentID="21" Title="Sub project 2"/>
<z:row ID="188" ParentID="187" Title="Sub project 2.1"/>
<z:row ID="189" ParentID="21" Title="Sub project 3"/>
<z:row ID="193" ParentID="1" Title="Sub project 4"/>
<z:row ID="194" ParentID="21" Title="Sub project 5"/>
<z:row ID="195" ParentID="0" Title="Sub project 6"/>
</rs:data>
</xml>
MY XSLT TRANSFOR======================================
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema" exclude-result-prefixes="s dt rs z" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="/xml/rs:data/z:row[not(@ParentID = /xml/rs:data/z:row/@ID)]"/>
</xsl:template>
<xsl:template match="z:row">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="/xml/rs:data/z:row[@ParentID = current()/@ID]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
MY INTENDED OUTPUT======================================
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly">
<s:AttributeType name="ID" rs:number="1" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="ParentID" rs:number="2" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true"/>
</s:AttributeType>
<s:AttributeType name="Title" rs:number="4" rs:nullable="true" rs:maydefer="true" rs:writeunknown="true">
<s:datatype dt:type="string" dt:maxLength="100"/>
</s:AttributeType>
<s:extends type="rs:rowbase"/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row ID="1" ParentID="0" Title="Security">
<z:row ID="30" ParentID="1" Title="Very nice">
<z:row ID="109" ParentID="30" Title="Using Event mg"/>
</z:row>
<z:row ID="193" ParentID="1" Title="Sub project 4"/>
</z:row>
<z:row ID="185" ParentID="21" Title="Sub project 1">
<z:row ID="186" ParentID="185" Title="Sub project 1.1"/>
</z:row>
<z:row ID="187" ParentID="21" Title="Sub project 2">
<z:row ID="188" ParentID="187" Title="Sub project 2.1"/>
</z:row>
<z:row ID="189" ParentID="21" Title="Sub project 3"/>
<z:row ID="194" ParentID="21" Title="Sub project 5"/>
<z:row ID="195" ParentID="0" Title="Sub project 6"/>
</rs:data>
</xml>