XSL muenchian-分组在多个级别和嵌套 [英] XSL muenchian-grouping on multiple levels and nesting
问题描述
输入:
<?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分组,但似乎无法解决这一问题. 我想实现的是这样:
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:
- 对于每个唯一的TRAILER/ID,将整个节点复制到输出中
-
如果预告片/ID的查询节点具有相同的参数/ID,则使用以下规则复制预告片节点并在其中构建配置节点:
- for each unique TRAILER/ID, copy that whole node in the output
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:
- 配置/ID-使用"configurationCode"中的值填充此标记(每个查询唯一)
- 对于查询中的每个唯一"compartmentCode"值,构建隔离专区节点
- 每个spacerCode可以具有一个或多个"unitNames"和"compartmentCapacity"值.我想要为每个值构建单独的节点,如期望的输出所示.
- 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
谢谢.
推荐答案
在这里您需要一个连接键,因为您希望在
configurationCode
上下文中使用不同的compartmentCode
值,因此您的键应如下所示: >You need a concatenated key here, as you want distinct
compartmentCode
values within the context ofconfigurationCode
, so your key would look like this<xsl:key name="queries" match="record" use="concat(column[@name='configurationCode'], '|', column[@name='compartmentCode'])"/>
然后在
query
中获取唯一的spacerCode值,请执行此操作...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])]">
尝试一下(请注意,我如何将某些代码分离到与
query
匹配的模板中,以避免过多的代码嵌套,并使某些xpath更简单)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>
这篇关于XSL muenchian-分组在多个级别和嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!