XSL muenchian-多层分组和嵌套

2024-03-06

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>

我尝试过阅读一些关于这种慕尼黑分组的内容,但似乎无法超越这一点。 我想要实现的是:

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

    • 配置/ID - 使用“configurationCode”中的值填充此标签(每个查询都是唯一的)
    • for each unique 'compartmentCode' value from the query, build the compartment node
      • 每个隔间代码可以有一个或多个“unitNames”和“compartmentCapacity”值。我想为每个值构建单独的节点,如所需的输出所示。

我离这个结果还有很长的路要走,但如果有人能帮助我,请帮助我

谢谢。


您在这里需要一个连接键,因为您想要不同的compartmentCode上下文中的值configurationCode,所以你的密钥看起来像这样

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

然后,在一个query,要获取唯一的隔间代码值,请执行以下操作...

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XSL muenchian-多层分组和嵌套 的相关文章

随机推荐

  • 如何重置参数值以便代理可以再次请求参数?

    我创建了一个对话流代理和其中的多个意图 其中一种意图需要 2 个参数 账户和余额类型 然后 它发送 Webhook 请求 其中写入代码以检查数据库中是否存在帐号 如果是的话 它会获取余额并发回给用户 现在 如果帐号不存在 我必须要求用户再次
  • 使用 json4s 序列化和反序列化 scala 枚举或 case 对象

    假设我有一个枚举或密封的案例对象组 如下所示 sealed abstract class Status case object Complete extends Status case object Failed extends Statu
  • Oracle ORDER BY 中忽略与号 (&) 字符

    我正在 Oracle 中运行一个查询 该查询按可能包含带有 号的值的列排序 然而 排序算法似乎忽略了 符号 例如 select from select txt from dual union select P txt from dual u
  • 基于查询的触发器是原子的吗?

    我有一个带有序列号的表 此序列号将会更改 并且引用自动编号将不起作用 我担心触发器的值会发生冲突 如果两个事务同时读取 我已经对 3 个连接运行了模拟测试 每个连接约 100 万条记录 没有发生冲突 CREATE TABLE aut au
  • 如何将类实例分配给变量并在其他类中使用它

    我正在做一些 Python 的基本练习 在这里 我定义了 3 个类 现在 我需要在另一个类中传递第一个类的实例 并在最后一个类中使用它 我写了如下代码 defining first class class MobileInventory d
  • 如何检查 Activity 是否存在多个实例

    我一直在尝试自学 Android 并且刚刚了解到我一直在创建相同的多个实例Activity在我的代码中 从概念上讲 我知道它在那里 但是有没有我可以使用的工具 例如 DDMS see我创建了多个实例 这也将帮助我了解我的实验是否Intent
  • 在 ASP.NET 中设置电子商务 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我是电子商务新手 我正在寻找一个关于为 ASP NET 设置休闲 ECom 或将其插入现有站点 的特殊指南 其中包含产品目录 购物车 商家
  • JavaScript,执行函数

    我有下一个功能 var hideAll function code return 我在另一个函数中像回调一样使用这个函数 当我像这样使用它时 function params hideAll 一切正常 但是当我使用时 function par
  • Angular 2 取消未经身份验证的路线导航

    我有一个应用程序 有 3 个链接 主页 登录 user sign in 和用户详细信息 user 当用户单击 主页 时 我的应用程序将公开显示内容 当用户单击 用户详细信息 时 我的应用程序将显示弹出登录窗口 如果用户取消登录弹出窗口 我的
  • 如何计算3组或更多组之间的SMD?

    我有兴趣通过一个分层变量计算成对标准化平均差 SMD 通常这是在两组之间计算 但是我们可以在 3 组或更多组中进行计算吗 附 我是 gtsummary 包的忠实粉丝 所以我尝试使用以下方法进行此分析示例 2 来自这个令人惊奇的包 http
  • elisp 警告“引用自由变量”

    我正在徘徊如何摆脱 elisp 警告 我的设置如下 我有 init el 文件设置 emacs root 变量 root of all emacs related stuff defvar emacs root if or eq syste
  • Amazon VPC n^2 -4 IP 地址? CIDR 块

    我正在为我的实例创建一个新的 AWS VPC 但是 我注意到 当我使用 CIDR 表示法创建 VPC 和公共子网时 AWS 指示我有 n 2 4 其中 n 是位数 可用 IP 地址 为什么是这样 据我了解 当n 2 2发生时 通常是去除位全
  • R 的时间差

    我有一些数据 如下所示 id time 1 2013 02 04 02 20 59 1 2013 02 04 02 21 05 1 2013 02 04 02 21 24 2 2013 02 04 02 21 26 2 2013 02 04
  • 403 禁止 Unity3D C#

    我一直在尝试从此链接获取信息 隐藏 这是我当前的代码 public static string newsLink HIDDEN public static readonly List
  • iOS 8 模拟器无法启动

    当我尝试在模拟器 iOS 8 0 上使用 Xcode 6 Beta 4 运行我的应用程序时 我总是收到此错误 无法启动 iOS 模拟器 其实我在这个网站上发现了一些类似的问题 我尝试了所有的答案 但仍然无法解决 我在 Xcode 6 Bet
  • 编写音频源过滤器以用作 Lync 麦克风

    我正在尝试编写一个 DirectShow 音频捕获过滤器 它可以被 Microsoft Lync 客户端识别为Microphone来源 这些是我已经采取的步骤 过滤器源自CSource 其输出引脚源自CSourceStream并实施IKsP
  • GrivView 的 BaseAdapter 中的绝对位置

    我正在尝试写BaseAdapter to my GrivView but in public View getView int position View convertView ViewGroup parent the position渲
  • 暗示类正在实现协议的正确方法?

    正在改进我的 Python 开发工作 我有兴趣在 CI deb 构建时测试使用 Protocol 定义的接口 这样 如果接口实际上没有由类实现 我们将在单元测试运行后立即知道 我的方法是使用 Protocol 进行输入并使用 Impleme
  • 作为非交互式 Rscript 运行时出现错误时停止 R 代码执行

    如何让 R 版本 3 4 2 CentOS 6 9 脚本在错误时停止而不是愉快地继续 我的文件 cat tmp R a 9 print a print b print d print Should never get here Runnin
  • XSL muenchian-多层分组和嵌套

    INPUT