【发布时间】:2020-04-22 10:54:27
【问题描述】:
我正在尝试根据一个字段在剩余记录中具有相同数据的条件对 XML 记录进行分组。我们将收到包含文档编号的 A_ROW 详细信息,并且我已将 B_ROW 记录分组在具有相同文档编号的 A_ROW 下。任何人都可以帮助阅读和分组 xslt 中的 xml 记录。
<recordSet>
<A_Row><company_code>1234</company_code>
<document_num>5606</document_num>
<document_type>CS</document_type>
<doc_date>20190206</doc_date>
</A_Row>
<B_Row>
<document_num>5606</document_num>
<pos>001</pos>
<account>4564343</account>
<account_type>ss</account_type>
</B_Row>
<B_Row>
<document_num>5606</document_num>
<pos>001</pos>
<account>4564344</account>
<account_type>ss</account_type>
</B_Row>
<A_Row><company_code>1234</company_code>
<document_num>5607</document_num>
<document_type>CS</document_type>
<doc_date>20190206</doc_date>
</A_Row>
<B_Row>
<document_num>5607</document_num>
<pos>001</pos>
<account>4564346</account>
<account_type>ss</account_type>
</B_Row>
<B_Row>
<document_num>5607</document_num>
<pos>001</pos>
<account>4564342</account>
<account_type>ss</account_type>
</B_Row>
<A_Row>
<company_code>1234</company_code>
<document_num>5608</document_num>
<document_type>CS</document_type>
<doc_date>20190206</doc_date>
</A_Row>
<B_Row>
<document_num>5608</document_num>
<pos>001</pos>
<account>4564349</account>
<account_type>ss</account_type>
</B_Row>
</recordSet>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns="http://springer.com/xi/SAP/MEDIASUITE">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Recordset">
<xsl:for-each select="BT_ROW">
<xsl:element name="BT_ROW">
<xsl:apply-templates/>
<xsl:variable name="document" select="/ns:MT_MEDIASUITE_ORDER_DATA/Recordset/BT_ROW/document_number"/>
<xsl:for-each select="/ns:MT_MEDIASUITE_ORDER_DATA/Recordset/BS_ROW">
<xsl:if test="/ns:MT_MEDIASUITE_ORDER_DATA/Recordset/BS_ROW/document_number =$document">
<xsl:element name="BS_ROW">
<xsl:apply-templates/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
预期结果:
<recordSet>
<A_Row><company_code>1234</company_code>
<document_num>5606</document_num>
<document_type>CS</document_type>
<doc_date>20190206</doc_date>
<B_Row>
<document_num>5606</document_num>
<pos>001</pos>
<account>4564343</account>
<account_type>ss</account_type>
</B_Row>
<B_Row>
<document_num>5606</document_num>
<pos>001</pos>
<account>4564344</account>
<account_type>ss</account_type>
</B_Row>
</A_Row>
<A_Row>
<company_code>1234</company_code>
<document_num>5607</document_num>
<document_type>CS</document_type>
<doc_date>20190206</doc_date>
<B_Row>
<document_num>5607</document_num>
<pos>001</pos>
<account>4564346</account>
<account_type>ss</account_type>
</B_Row>
<B_Row>
<document_num>5607</document_num>
<pos>001</pos>
<account>4564342</account>
<account_type>ss</account_type>
</B_Row>
</A_Row>
<A_Row>
<company_code>1234</company_code>
<document_num>5608</document_num>
<document_type>CS</document_type>
<doc_date>20190206</doc_date>
<B_Row>
<document_num>5608</document_num>
<pos>001</pos>
<account>4564349</account>
<account_type>ss</account_type>
</B_Row>
</A_Row>
</recordSet>
【问题讨论】:
-
所以你使用 XSLT 2 但你不知道如何使用
for-each-group? -
我不熟悉for-each-group
标签: xslt xslt-1.0 xslt-2.0 xslt-grouping