xslt - 多层次的慕尼黑分组
问题描述
我知道有一种更简单的方法可以做到这一点,但现在我仅限于使用 XSLT1.0,所以我必须使用 Muenchian Grouping。
我有以下 XML:
<Root xmlns="http://MuleSoft.Attraqt.FlatFileSchema1">
<Root_Child1 xmlns="">
<sku>123456777</sku>
<ProductMaster>123456</ProductMaster>
<default>yes</default>
<category>UK</category>
<display_name>UK Web Categories</display_name>
</Root_Child1>
<Root_Child1 xmlns="">
<sku>123456777</sku>
<ProductMaster>123456</ProductMaster>
<default>no</default>
<category>DE</category>
<display_name>DE Web Categories</display_name>
</Root_Child1>
<Root_Child1 xmlns="">
<sku>123456999</sku>
<ProductMaster>123456</ProductMaster>
<default>yes</default>
<category>UK</category>
<display_name>UK Web Categories</display_name>
</Root_Child1>
<Root_Child1 xmlns="">
<sku>123456999</sku>
<ProductMaster>123456</ProductMaster>
<default>no</default>
<category>DE</category>
<display_name>DE Web Categories</display_name>
</Root_Child1>
<Root_Child1 xmlns="">
<sku>987654333</sku>
<ProductMaster>987654</ProductMaster>
<default>no</default>
<category>UK</category>
<display_name>UK Web Categories</display_name>
</Root_Child1>
<Root_Child1 xmlns="">
<sku>987654333</sku>
<ProductMaster>987654</ProductMaster>
<default>no</default>
<category>DE</category>
<display_name>DE Web Categories</display_name>
</Root_Child1>
<Root_Child1 xmlns="">
<sku>987654333</sku>
<ProductMaster>987654</ProductMaster>
<default>yes</default>
<category>GLOBAL</category>
<display_name>GLOBAL Web Categories</display_name>
</Root_Child1>
</Root>
我想将其转换为这种格式:
<?xml version='1.0' encoding='UTF-8'?>
<records>
<record>
<fields>
<field name="ProductMaster">
<values>
<value>123456</value>
</values>
</field>
<field name="size">
<attributes>
<attribute>
<field name="sku">
<values>
<value>123456777</value>
</values>
</field>
</attribute>
<attribute>
<field name="sku">
<values>
<value>123456999</value>
</values>
</field>
</attribute>
</attributes>
</field>
</fields>
<categories>
<categorytree>
<category default="yes" uniqueid="UK">UK Web Categories</category>
</categorytree>
<categorytree>
<category default="no" uniqueid="DE">DE Web Categories</category>
</categorytree>
</categories>
</record>
<record>
<fields>
<field name="ProductMaster">
<values>
<value>987654</value>
</values>
</field>
<field name="size">
<attributes>
<attribute>
<field name="sku">
<values>
<value>987654333</value>
</values>
</field>
</attribute>
</attributes>
</field>
</fields>
<categories>
<categorytree>
<category default="no" uniqueid="UK">UK Web Categories</category>
</categorytree>
<categorytree>
<category default="no" uniqueid="DE">DE Web Categories</category>
</categorytree>
<categorytree>
<category default="yes" uniqueid="GLOBAL">GLOBAL Web Categories</category>
</categorytree>
</categories>
</record>
</records>
基本上在每个<record></record>
块中应该有 1 个 ProductMaster(我可以得到)。然后<attributes></attributes>
它需要在 ProductMaster 中循环并显示每个 sku(但只有一次,目前我的只显示一个 sku)然后在下一个<categorytree></categorytree>
它需要显示每个类别(也只有一次,目前我的显示重复)。我从下面的 XSLT1.0 开始,它让我第一次<record></record>
正确分组,但我不知道如何正确获得接下来的 2 个分组。请注意,我创建了另外 2 个键来提供帮助,但我不知道如何使用它们:
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:s0="http://MuleSoft.Attraqt.FlatFileSchema1">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:key name="groupbyproduct" match="Root_Child1" use="ProductMaster"/>
<xsl:key name="groupbysku" match="Root_Child1" use="concat(ProductMaster,'|',sku)"/>
<xsl:key name="groupbycategory" match="Root_Child1" use="concat(ProductMaster,'|',category)"/>
<xsl:template match="/s0:Root">
<records>
<xsl:for-each select="Root_Child1[count(. | key('groupbyproduct', ProductMaster)[1]) = 1]">
<record>
<fields>
<field name="ProductMaster">
<values>
<value>
<xsl:value-of select="ProductMaster/text()"/>
</value>
</values>
</field>
<field name="size">
<attributes>
<attribute>
<field name="sku">
<values>
<value>
<xsl:value-of select="sku/text()"/>
</value>
</values>
</field>
</attribute>
</attributes>
</field>
</fields>
<categories>
<!-- for each member of current group -->
<xsl:for-each select="key('groupbyproduct', ProductMaster)">
<categorytree>
<category default="{default}" uniqueid="{category}">
<xsl:value-of select="display_name/text()"/>
</category>
</categorytree>
</xsl:for-each>
</categories>
</record>
</xsl:for-each>
</records>
</xsl:template>
</xsl:stylesheet>```
解决方案
推荐阅读
- python - 如何在所有文件中搜索字符串
- json - 是否可以从 statusjson.cgi 中得知 Nagios 监控的服务在 Nagios 上处于静默状态?
- python - ZipImportError 而工具链为 kivy-ios 编程构建 python3
- javascript - 如何在反应钩子中将单词进一步拆分为字母
- c# - Unity InputSystem 值持续输出 0,除非它从执行事件中读取
- matlab - 实施卡尔曼滤波器时设计矩阵 H 不正确
- sql - SQL:选择员工的时间段 COUNT()
- python - 无法从 Windows 中的 docker 容器执行 .sh 文件
- python - DRF 序列化程序未从对 ManyToMany 字段的请求中收到值
- css - 如何在 CSS 中设置左侧面板 100% 高度?