需要Extreme XSLT 1 Flat to Hierarchical Transform

时间:2014-11-11 21:43:19

标签: xslt hierarchical flat

我正在努力将平面XML转换为分层XML。我也坚持使用XSLT 1.0。我的实际情况非常复杂,但我认为我可以将其减少到这样的程度:

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

它需要成为:

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

虽然我知道我需要创建一些键,但我无法找到适当的匹配来提取和映射数据。

我真的希望得到一些帮助。

1 个答案:

答案 0 :(得分:0)

  

我真的很喜欢一些帮助入门。

以此为出发点:

<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>
        <!-- continue from here... -->

    </tns:customers>
</xsl:template>


</xsl:stylesheet>

我不确定如何继续这一点,即使我想:我也没有看到每个订单有多个客户,正如我在评论中所说,订单和访问之间的关系也不太清楚。