XSL muenchian-grouping在多个级别和嵌套

时间:2018-05-09 20:18:24

标签: xml xslt xslt-grouping muenchian-grouping

INPUT:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<output>
    <queries>
        <query>
            <parameters>
                <parameter name="id">CTL-000002</parameter>
            </parameters>
            <queryResults>
                <record id="1">
                    <column name="ConfigurationCapacity">9500.0000000</column>
                    <column name="configurationCode">CTL-3819</column>
                    <column name="compartmentCode">CTL-3819-01</column>
                    <column name="position">1</column>
                    <column name="CompartmentCapacity">2700</column>
                    <column name="unitName">G</column>
                </record>
                <record id="2">
                    <column name="ConfigurationCapacity">52120.0000000</column>
                    <column name="configurationCode">CTL-3819</column>
                    <column name="compartmentCode">CTL-3819-01</column>
                    <column name="position">1</column>
                    <column name="CompartmentCapacity">22950</column>
                    <column name="unitName">K</column>
                </record>
                <record id="3">
                    <column name="ConfigurationCapacity">9500.0000000</column>
                    <column name="configurationCode">CTL-3819</column>
                    <column name="compartmentCode">CTL-3819-02</column>
                    <column name="position">2</column>
                    <column name="CompartmentCapacity">1700</column>
                    <column name="unitName">G</column>
                </record>
            </queryResults>
        </query>
    </queries>
</output>
<trailer>
    <id>CTL-000002</id>
    <trailer_tag>0</trailer_tag>
</trailer>
<output>
    <queries>
        <query>
            <parameters>
                <parameter name="id">3</parameter>
            </parameters>
            <queryResults>
                <record id="1">
                    <column name="ConfigurationCapacity">12</column>
                    <column name="configurationCode">LT</column>
                    <column name="compartmentCode">3819-01</column>
                    <column name="position">1</column>
                    <column name="CompartmentCapacity">70</column>
                    <column name="unitName">G</column>
                </record>
                <record id="2">
                    <column name="ConfigurationCapacity">500</column>
                    <column name="configurationCode">LT</column>
                    <column name="compartmentCode">3819-01</column>
                    <column name="position">1</column>
                    <column name="CompartmentCapacity">20</column>
                    <column name="unitName">K</column>
                </record>
            </queryResults>
        </query>
    </queries>
</output>
<trailer>
    <id>3</id>
    <trailer_tag>0</trailer_tag>
</trailer>
</root>

XSL:

  <xsl:key name="queries" match="root/output/queries/query/queryResults/record" use="./column[@name='compartmentCode']"/>
<xsl:template match="@* | node()">
    <xsl:variable name="uniqueCompartment" select="//record[string(column[@name='compartmentCode'])][count(. | key('queries', column[@name='compartmentCode'])[1]) = 1]"/>
    <root>
        <xsl:for-each select="//trailer">
            <xsl:choose>
                <xsl:when test="trailer_tag='0'">
                    <configurations>
                        <configuration>
                            <id>
                                <xsl:value-of select="//root/output/queries/query[parameters/parameter[@name='id'] = current()/id]/queryResults/record/column[@name='configurationCode']"/>
                            </id>
                            <compartments>
                                <!--I need to build the following structure for each unique compartmentCode-->
                                <xsl:for-each select="//root/output/queries/query/queryResults/record/column[@name='compartmentCode'][not(.=preceding::*)]">
                                    <compartment>
                                        <code>
                                            <xsl:value-of select="."/>
                                        </code>
                                        <capacities>
                                            <xsl:for-each select="$uniqueCompartment">
                                                <capacity>
                                                    <!--I need for each unique Compartment to build the unit node for each unique UNIT that specific record has, and another node with the value of compartmentCapacity of that UNIT-->
                                                    <unit>
                                                    </unit>
                                                    <val>
                                                    </val>
                                                </capacity>
                                            </xsl:for-each>
                                        </capacities>
                                    </compartment>
                                </xsl:for-each>
                            </compartments>
                        </configuration>
                    </configurations>
                    <!--copy  trailer node-->
                    <xsl:copy-of select="."/>
                </xsl:when>
                <xsl:otherwise>
                    <!--something else-->
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </root>
    </xsl:template>

期望的输出:

<root>
<trailer>
    <id>CTL-000002</id>
    <trailer_tag>0</trailer_tag>
    <configurations>
        <configuration>
            <id>CTL-3819</id>
            <compartments>
                <compartment>
                    <code>CTL-3819-01</code>
                    <capacities>
                        <capacity>
                            <unit>G</unit>
                            <val>2700</val>
                        </capacity>
                        <capacity>
                            <unit>KG</unit>
                            <val>22950</val>
                        </capacity>
                    </capacities>
                </compartment>
                <compartment>
                    <code>CTL-3819-02</code>
                    <capacities>
                        <capacity>
                            <unit>G</unit>
                            <val>1700</val>
                        </capacity>
                    </capacities>
                </compartment>
            </compartments>
        </configuration>
    </configurations>
</trailer>
<trailer>
    <id>3</id>
    <trailer_tag>0</trailer_tag>
    <configurations>
        <configuration>
            <id>LT</id>
            <compartments>
                <compartment>
                    <code>3819-01</code>
                    <capacities>
                        <capacity>
                            <unit>G</unit>
                            <val>70</val>
                        </capacity>
                        <capacity>
                            <unit>K</unit>
                            <val>20</val>
                        </capacity>
                    </capacities>
                </compartment>
            </compartments>
        </configuration>
    </configurations>
</trailer>
</root>

我已经尝试过对这种muenchian分组的一些阅读,但似乎无法超越这一点。 我想要实现的是:

  1. 对于每个唯一的TRAILER / ID,复制输出中的整个节点
  2. 如果预告片/ ID具有相同参数/ ID的查询节点,则使用以下规则复制预告片节点并在其中构建配置节点:

    • 配置/ ID - 使用来自&#39; configurationCode&#39;的值填充此标记。 (这个是每个查询唯一的)
    • 对于每个独特的“隔离码”#39;查询中的值,构建隔离专区节点
      • 每个compartCode可以有一个或多个“unitNames”。和&#39; compartmentCapacity&#39;值。我想用每个值构建单独的节点,如所需的输出所示。
  3. 我距离这个结果还有很长的路要走,但是,如果有人能帮助我,请取悦

    谢谢。

1 个答案:

答案 0 :(得分:2)

您需要一个连锁密钥,因为您需要在compartmentCode的上下文中使用不同的configurationCode值,因此您的密钥将如下所示

<xsl:key name="queries" 
         match="record" 
         use="concat(column[@name='configurationCode'], '|', column[@name='compartmentCode'])"/>

然后,在query内,要获取唯一的compartmentCode值,请执行此操作...

<xsl:for-each 
     select="queryResults/record[generate-id() = generate-id(key('queries', concat(column[@name='configurationCode'], '|', column[@name='compartmentCode']))[1])]">

试试这个(注意我是如何将一些代码分成匹配query的模板以避免过多的代码嵌套,并使一些xpath更简单)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:key name="queries" match="record" use="concat(column[@name='configurationCode'], '|', column[@name='compartmentCode'])"/>

<xsl:template match="/">
    <root>
        <xsl:for-each select="//trailer">
            <xsl:choose>
                <xsl:when test="trailer_tag='0'">
                    <xsl:copy>
                        <!--copy  trailer node-->
                        <xsl:copy-of select="@*|node()"/>
                        <xsl:apply-templates select="//root/output/queries/query[parameters/parameter[@name='id'] = current()/id]" />
                    </xsl:copy>
                </xsl:when>
                <xsl:otherwise>
                    <!--something else-->
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </root>
</xsl:template>

<xsl:template match="query">
    <configurations>
        <configuration>
            <id>
                <xsl:value-of select="queryResults/record/column[@name='configurationCode']"/>
            </id>
            <compartments>
                <!--I need to build the following structure for each unique compartmentCode-->
                <xsl:for-each select="queryResults/record[generate-id() = generate-id(key('queries', concat(column[@name='configurationCode'], '|', column[@name='compartmentCode']))[1])]">
                    <compartment>
                        <code>
                            <xsl:value-of select="column[@name='compartmentCode']"/>
                        </code>
                        <capacities>
                            <xsl:for-each select="key('queries', concat(column[@name='configurationCode'], '|', column[@name='compartmentCode']))">
                                <capacity>
                                    <unit>
                                        <xsl:value-of select="column[@name='unitName']"/>
                                    </unit>
                                    <val>
                                        <xsl:value-of select="column[@name='CompartmentCapacity']"/>
                                    </val>
                                </capacity>
                            </xsl:for-each>
                        </capacities>
                    </compartment>
                </xsl:for-each>
            </compartments>
        </configuration>
    </configurations>    
</xsl:template>

</xsl:stylesheet>