基于不同行项目的分组 xsl1.0
Posted
技术标签:
【中文标题】基于不同行项目的分组 xsl1.0【英文标题】:grouping based on non-identical line items xsl1.0 【发布时间】:2020-11-24 16:04:45 【问题描述】:我有如下要求,仅使用 XSL1.0 实现转换,
-
第一个条件是根据 parentLineNumber 对订单项进行分组。
那么第二个条件是当 ParentLinenumber 和 LineNumber 相同时,仅当组有多个发票行项目时忽略发票行。
示例输入:
<InvoiceNotification>
<Invoice>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000010</parentLineNumber>
</LineSection>
<LineNumber>000010</LineNumber>
<proprietaryInformation>
<FreeFormText>PK06</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000010</parentLineNumber>
</LineSection>
<LineNumber>000011</LineNumber>
<proprietaryInformation>
<FreeFormText>PK07</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000010</parentLineNumber>
</LineSection>
<LineNumber>000012</LineNumber>
<proprietaryInformation>
<FreeFormText>PK08</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000020</parentLineNumber>
</LineSection>
<LineNumber>000020</LineNumber>
<proprietaryInformation>
<FreeFormText>GK01</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
</Invoice>
</InvoiceNotification>
我在 XSLT 下开发了部分工作。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="Invoices" match="InvoiceLineItem" use="LineSection/parentLineNumber"/>
<xsl:template match="InvoiceNotification">
<Invoices>
<xsl:for-each select="Invoice/InvoiceLineItem [ count ( key('Invoices',LineSection/parentLineNumber)[1] | . ) = 1 ]">
<Batchorder>
<xsl:for-each select="key('Invoices',LineNumber)">
<Items>
<LineItem>
<xsl:value-of select="proprietaryInformation"/>
</LineItem>
</Items>
</xsl:for-each>
</Batchorder>
</xsl:for-each>
</Invoices>
</xsl:template>
</xsl:stylesheet>
结果输出:
<?xml version="1.0" encoding="UTF-8"?>
<Invoices>
<Batchorder>
<Items>
<proprietaryInformation>PK06</proprietaryInformation>
</Items>
<Items>
<proprietaryInformation>PK07</proprietaryInformation>
</Items>
<Items>
<proprietaryInformation>PK08</proprietaryInformation>
</Items>
</Batchorder>
<Batchorder>
<Items>
<proprietaryInformation>GK01</proprietaryInformation>
</Items>
</Batchorder>
</Invoices>
但我期望低于输出,
<?xml version="1.0" encoding="UTF-8"?>
<Invoices>
<Batchorder>
<Items>
<proprietaryInformation>PK07</proprietaryInformation>
</Items>
<Items>
<proprietaryInformation>PK08</proprietaryInformation>
</Items>
</Batchorder>
<Batchorder>
<Items>
<proprietaryInformation>GK01</proprietaryInformation>
</Items>
</Batchorder>
</Invoices>
【问题讨论】:
【参考方案1】:我认为你可以在for-each
上使用谓词:
<xsl:for-each select="key('Invoices',LineNumber)[LineNumber != LineSection/parentLineNumber or count(key('Invoices', LineNumber)) = 1]">
【讨论】:
我认为如果不对键应用类似的谓词,这将无法工作。但是我没有完全理解 OP 的要求,所以我可能是错的。【参考方案2】:与 Martin Honnen 提出的解决方案几乎相同,但可能更高效一些:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="Invoices" match="InvoiceLineItem" use="LineSection/parentLineNumber"/>
<xsl:template match="InvoiceNotification">
<Invoices>
<xsl:for-each select=
"Invoice/InvoiceLineItem
[count ( key('Invoices',LineSection/parentLineNumber)[1] | . ) = 1]">
<Batchorder>
<xsl:for-each select="key('Invoices',LineNumber)
[not(LineNumber = LineSection/parentLineNumber)
or not(key('Invoices',LineNumber)[2])]">
<Items>
<LineItem>
<xsl:value-of select="proprietaryInformation"/>
</LineItem>
</Items>
</xsl:for-each>
</Batchorder>
</xsl:for-each>
</Invoices>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的源 XML 文档时:
<InvoiceNotification>
<Invoice>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000010</parentLineNumber>
</LineSection>
<LineNumber>000010</LineNumber>
<proprietaryInformation>
<FreeFormText>PK06</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000010</parentLineNumber>
</LineSection>
<LineNumber>000011</LineNumber>
<proprietaryInformation>
<FreeFormText>PK07</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000010</parentLineNumber>
</LineSection>
<LineNumber>000012</LineNumber>
<proprietaryInformation>
<FreeFormText>PK08</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
<InvoiceLineItem>
<LineSection>
<parentLineNumber>000020</parentLineNumber>
</LineSection>
<LineNumber>000020</LineNumber>
<proprietaryInformation>
<FreeFormText>GK01</FreeFormText>
</proprietaryInformation>
</InvoiceLineItem>
</Invoice>
</InvoiceNotification>
产生想要的正确结果:
<Invoices>
<Batchorder>
<Items>
<LineItem>
PK07
</LineItem>
</Items>
<Items>
<LineItem>
PK08
</LineItem>
</Items>
</Batchorder>
<Batchorder>
<Items>
<LineItem>
GK01
</LineItem>
</Items>
</Batchorder>
</Invoices>
【讨论】:
感谢 Dimitre,是的,这按预期工作。感谢您抽出宝贵时间在这里提供帮助!以上是关于基于不同行项目的分组 xsl1.0的主要内容,如果未能解决你的问题,请参考以下文章
基于列表中的日期字段,在每一天对使用XSLT 1.0的列表进行分组