【发布时间】:2022-01-01 23:54:16
【问题描述】:
我正在尝试使用基于 RAPID_ID 的每个组逻辑的 xslt 将输入 xml 值转换为输出 xml
Input.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<Output>
<ID>1234</ID>
<CustomerName>KUMAR</CustomerName>
<BranchName>HARBOUR</BranchName>
<SchemeName>GOLD</SchemeName>
<MobileNumber>123456789</MobileNumber>
<CustomerType>PRIMARY</CustomerType>
<DedupeFound>NO</DedupeFound>
</Output>
<Output>
<ID>1234</ID>
<CustomerName>SEAN</CustomerName>
<BranchName>HARBOUR</BranchName>
<SchemeName>GOLD</SchemeName>
<MobileNumber>123456789</MobileNumber>
<CustomerType>SECONDARY</CustomerType>
<DedupeFound>YES</DedupeFound>
</Output>
<Output>
<ID>5678</ID>
<CustomerName>MARK</CustomerName>
<BranchName>CANTONMENT</BranchName>
<SchemeName>DIAMOND</SchemeName>
<MobileNumber>123456789</MobileNumber>
<CustomerType>PRIMARY</CustomerType>
<DedupeFound>NO</DedupeFound>
</Output>
<Output>
<ID>5678</ID>
<CustomerName>STEVE</CustomerName>
<BranchName>CANTONMENT</BranchName>
<SchemeName>DIAMOND</SchemeName>
<MobileNumber>123456789</MobileNumber>
<CustomerType>SECONDARY</CustomerType>
<DedupeFound>YES</DedupeFound>
</Output>
</Response>
我的预期输出是
输出.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Response>
<Output>
<ID>1234</ID>
<CustomerName>KUMAR</CustomerName>
<BranchName>HARBOUR</BranchName>
<SchemeName>GOLD</SchemeName>
<MobileNumber>123456789</MobileNumber>
<DedupeDetails>
<CustomerType>PRIMARY</CustomerType>
<CustomerName>KUMAR</CustomerName>
<DedupeFound>NO</DedupeFound>
</DedupeDetails>
<DedupeDetails>
<CustomerType>SECONDARY</CustomerType>
<CustomerName>SEAN</CustomerName>
<DedupeFound>YES</DedupeFound>
</DedupeDetails>
</Output>
<Output>
<ID>5678</ID>
<CustomerName>MARK</CustomerName>
<BranchName>CANTONMENT</BranchName>
<SchemeName>DIAMOND</SchemeName>
<MobileNumber>123456789</MobileNumber>
<DedupeDetails>
<CustomerType>PRIMARY</CustomerType>
<CustomerName>MARK</CustomerName>
<DedupeFound>NO</DedupeFound>
</DedupeDetails>
<DedupeDetails>
<CustomerType>SECONDARY</CustomerType>
<CustomerName>STEVE</CustomerName>
<DedupeFound>YES</DedupeFound>
</DedupeDetails>
</Output>
</Response>
我从这样的事情开始,但无法继续进行。 我试图首先对 ID 参数进行分组,在里面它从主要客户详细信息开始。 在主要客户详细信息之后,我必须迭代每个客户(这里主要和次要)
任何建议/更正以实现这一目标。
我的 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Response>
<xsl:for-each-group select="/Response/Output" group-by="ID">
<xsl:sort select="ID"/>
</xsl:for-each-group>
</Response>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
抱歉,您的 XML 示例中没有
RAPID_ID,因此 XSLT 对于您显示的输入示例没有任何意义。 -
@MartinHonnen 道歉。我已经更正了 xslt。我最初粘贴了不同版本的 xslt。
-
好吧。
<xsl:for-each-group select="/Response/Output" group-by="ID">看起来不错,但是如果你不创建任何内容就没有任何内容,所以至少<xsl:copy>...</xsl:copy>将构成for-each-group的内容,为每个组创建一个输出。然后根据需要填充您需要的元素并根据current-group()处理组中的不同项目
标签: xslt xslt-grouping