XSL muenchian-grouping on multiple levels and nesting

拟墨画扇 提交于 2019-12-10 22:17:53

问题


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>

DESIRED OUTPUT:

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

I have tried some reading on this kind of muenchian grouping, but can't seem to work past this point. What I want to achieve is this:

  1. for each unique TRAILER/ID, copy that whole node in the output
  2. IF the trailer/ID has a query node with the same parameter/ID, then copy the trailer node and build inside it the configurations node using the following rules:

    • configuration/ID - populate this tag with the value from 'configurationCode' (this one is unique per query)
    • for each unique 'compartmentCode' value from the query, build the compartment node
      • each compartmentCode can have one or more 'unitNames' and 'compartmentCapacity' values. I want to build separate nodes with each value, as seen in the desired output.

I'm a long way from this result, but please if anyone could help me

Thank you.


回答1:


You need a concatenated key here, as you want distinct compartmentCode values within the context of configurationCode, so your key would look like this

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

Then, within a query, to get the unique compartmentCode values, do this...

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

Try this (note how I have separated some code out into a template matching query to avoid too much nesting of code, and to make some xpaths simpler)

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


来源:https://stackoverflow.com/questions/50261381/xsl-muenchian-grouping-on-multiple-levels-and-nesting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!