Monday 15 September 2014

hierarchical - Extreme XSLT 1 Flat to Hierarchial Transform needed -



hierarchical - Extreme XSLT 1 Flat to Hierarchial Transform needed -

i fighting pretty extreme case of transforming flat xml hierarchical one. i'm stuck using xslt 1.0. actual case pretty convoluted, think can cut down downwards this:

<?xml version="1.0" encoding="utf-8"?> <env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:header/> <env:body> <tns:getdatars xmlns:tns="http://www.myco.com/dataservice"> <tns:acknowledgement>process completed successfully.</tns:acknowledgement> <tns:customer> <tns:customerid>210</tns:customerid> <tns:visitid>12</tns:visitid> <tns:storeid>1</tns:storeid> <tns:storeorder>28</tns:storeorder> <tns:itemid>1</tns:itemid> <tns:customersalesdate>2014-09-26</tns:customersalesdate> </tns:customer> <tns:customer> <tns:customerid>210</tns:customerid> <tns:visitid>12</tns:visitid> <tns:storeid>1</tns:storeid> <tns:storeorder>28</tns:storeorder> <tns:itemid>3</tns:itemid> <tns:customersalesdate>2014-09-26</tns:customersalesdate> </tns:customer> <tns:customer> <tns:customerid>211</tns:customerid> <tns:visitid>31</tns:visitid> <tns:storeid>2</tns:storeid> <tns:storeorder>48</tns:storeorder> <tns:itemid>2</tns:itemid> <tns:customersalesdate>2014-09-26</tns:customersalesdate> </tns:customer> <tns:customer> <tns:customerid>211</tns:customerid> <tns:visitid>31</tns:visitid> <tns:storeid>2</tns:storeid> <tns:storeorder>48</tns:storeorder> <tns:itemid>4</tns:itemid> <tns:customersalesdate>2014-09-26</tns:customersalesdate> </tns:customer> <tns:item> <tns:customerid>210</tns:customerid> <tns:visitid>12</tns:visitid> <tns:storeid>1</tns:storeid> <tns:itemid>1</tns:itemid> <tns:unitprice>2.95</tns:unitprice> <tns:quantity>4</tns:quantity> </tns:item> <tns:item> <tns:customerid>211</tns:customerid> <tns:visitid>31</tns:visitid> <tns:storeid>1</tns:storeid> <tns:itemid>2</tns:itemid> <tns:unitprice>3.29</tns:unitprice> <tns:quantity>2</tns:quantity> </tns:item> <tns:item> <tns:customerid>210</tns:customerid> <tns:visitid>12</tns:visitid> <tns:storeid>2</tns:storeid> <tns:itemid>3</tns:itemid> <tns:unitprice>4.99</tns:unitprice> <tns:quantity>1</tns:quantity> </tns:item> <tns:item> <tns:customerid>211</tns:customerid> <tns:visitid>31</tns:visitid> <tns:storeid>2</tns:storeid> <tns:itemid>4</tns:itemid> <tns:unitprice>6.95</tns:unitprice> <tns:quantity>2</tns:quantity> </tns:item> </tns:getdatars> </env:body> </env:envelope>

and needs become:

<?xml version="1.0" encoding="utf-8"?> <env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:header/> <env:body> <tns:getdatars xmlns:tns="http://www.myco.com/dataservice"> <tns:acknowledgement>process completed successfully.</tns:acknowledgement> <tns:stores> <tns:store> <tns:storeid>1</tns:storeid> <tns:orders> <tns:order>28</tns:order> <tns:salesdate>2014-09-26</tns:salesdate> <tns:customers> <tns:customer> <tns:customerid>210</tns:customerid> <tns:visitid>12</tns:visitid> <tns:items> <tns:item> <tns:itemid>1</tns:itemid> <tns:unitprice>2.95</tns:unitprice> <tns:quantity>4</tns:quantity> </tns:item> <tns:item> <tns:itemid>3</tns:itemid> <tns:unitprice>4.99</tns:unitprice> <tns:quantity>1</tns:quantity> </tns:item> </tns:items> </tns:customer> </tns:customers> </tns:orders> </tns:store> <tns:store> <tns:storeid>2</tns:storeid> <tns:orders> <tns:order>48</tns:order> <tns:salesdate>2014-09-26</tns:salesdate> <tns:customers> <tns:customer> <tns:customerid>211</tns:customerid> <tns:visitid>31</tns:visitid> <tns:items> <tns:item> <tns:itemid>2</tns:itemid> <tns:unitprice>3.29</tns:unitprice> <tns:quantity>2</tns:quantity> </tns:item> <tns:item> <tns:itemid>4</tns:itemid> <tns:unitprice>6.95</tns:unitprice> <tns:quantity>2</tns:quantity> </tns:item> </tns:items> </tns:customer> </tns:customers> </tns:orders> </tns:store> </tns:stores> </tns:getdatars> </env:body> </env:envelope>

and though know need create number of keys, can't quite figure out proper matching extract , map data.

i'd help getting started.

i'd help getting started.

try starting point:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:tns="http://www.myco.com/dataservice"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="store" match="tns:storeid" use="." /> <xsl:key name="order-by-store" match="tns:storeorder" use="../tns:storeid" /> <xsl:key name="order-by-id" match="tns:storeorder" use="." /> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="tns:getdatars"> <xsl:copy> <xsl:copy-of select="tns:acknowledgement"/> <tns:stores> <xsl:apply-templates select="tns:customer/tns:storeid[count(. | key('store', .)[1]) = 1]"/> </tns:stores> </xsl:copy> </xsl:template> <xsl:template match="tns:storeid"> <tns:store> <xsl:copy-of select="."/> <tns:orders> <xsl:apply-templates select="key('order-by-store', .)[count(. | key('order-by-id', .)[1]) = 1]"/> </tns:orders> </tns:store> </xsl:template> <xsl:template match="tns:storeorder"> <tns:order><xsl:value-of select="."/></tns:order> <tns:customers> <!-- go on here... --> </tns:customers> </xsl:template> </xsl:stylesheet>

i not sure how go on point on, if wanted to: don't see have multiple customers per order, , said in comments, relationship between orders , visits not quite clear either.

xslt hierarchical flat

No comments:

Post a Comment