花了太多时间在这上面..“f $%” 希望您能分享您的经验。 我在以下结构中从外部数据库到达源平面XML:
<?xml version="1.0" encoding="utf-8"?>
<ns:MT_ActualCosts xmlns:ns="http://percite:scmaster/actual_costs">
<row>
<EVENT_ID>106</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>9999</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-09 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>0001</TOTAL_AMOUNT_NET>
<RO_NUMBER>102808</RO_NUMBER>
</row>
<row>
<EVENT_ID>106</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24444</INVOICE_NUMBER>
<PLANT>0003</PLANT>
<ALLOCATION_AMOUNT>122</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>109</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24458</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-09 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>0011</TOTAL_AMOUNT_NET>
<RO_NUMBER>102813</RO_NUMBER>
</row>
<row>
<EVENT_ID>109</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24458</INVOICE_NUMBER>
<PLANT>0003</PLANT>
<ALLOCATION_AMOUNT>11.1</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>108</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24535</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-19 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>11</TOTAL_AMOUNT_NET>
<RO_NUMBER>102811</RO_NUMBER>
</row>
<row>
<EVENT_ID>108</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24535</INVOICE_NUMBER>
<PLANT>0002</PLANT>
<ALLOCATION_AMOUNT>11</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-28 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>999999</TOTAL_AMOUNT_NET>
<RO_NUMBER>103063</RO_NUMBER>
</row>
<row>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<PLANT>0001</PLANT>
<ALLOCATION_AMOUNT>11.47</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>2</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<PLANT>0001</PLANT>
<ALLOCATION_AMOUNT>11.53</ALLOCATION_AMOUNT>
</row>
</ns:MT_ActualCosts>
My requested Target Structure should be something like this:
[![enter image description here][1]][1]
I need to group under the Header segments RecordLine Segments of the same `EVENT_ID`.
Currently my XSLT can't create the needed structure.
this is my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns0="http://percite:scmaster/actual_costs">
<xsl:output method="xml" indent="yes"/>
<!-- xsi:noNamespaceSchemaLocation="\\palnt03\palramnet-redirect$\IL-Users\My-Documents\nimrod_g\SAP\Projects\ScMaster\Finance\ActualCosts\development\xsd"> -->
<xsl:template match="/">
<ns0:MT_ActualCostPreNormalized>
<xsl:for-each select= "ns0:MT_ActualCosts/row">
<xsl:if test="LINE_NUMBER=0">
<Header>
<EVENT_ID><xsl:value-of select="EVENT_ID"></xsl:value-of>
</EVENT_ID>
<LINE_NUMBER>
<xsl:value-of select="LINE_NUMBER"/>
</LINE_NUMBER>
<INVOICE_NUMBER>
<xsl:value-of select="INVOICE_NUMBER"/>
</INVOICE_NUMBER>
<CURRENCY_CODE>
<xsl:value-of select="CURRENCY_CODE"/>
</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>
<xsl:value-of select="TOTAL_AMOUNT_NET"/>
</TOTAL_AMOUNT_NET>
<RO_NUMBER>
<xsl:value-of select="RO_NUMBER"/>
</RO_NUMBER>
<PLANT>
<xsl:value-of select="PLANT"/>
</PLANT>
<ALLOCATION_AMOUNT>
<xsl:value-of select="ALLOCATION_AMOUNT"/>
</ALLOCATION_AMOUNT>
</Header>
</xsl:if>
<xsl:if test="LINE_NUMBER!=0">
<RecordLine>
<EVENT_ID><xsl:value-of select="EVENT_ID"></xsl:value-of>
</EVENT_ID>
<LINE_NUMBER>
<xsl:value-of select="LINE_NUMBER"/>
</LINE_NUMBER>
<INVOICE_NUMBER>
<xsl:value-of select="INVOICE_NUMBER"/>
</INVOICE_NUMBER>
<CURRENCY_CODE>
<xsl:value-of select="CURRENCY_CODE"/>
</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>
<xsl:value-of select="TOTAL_AMOUNT_NET"/>
</TOTAL_AMOUNT_NET>
<RO_NUMBER>
<xsl:value-of select="RO_NUMBER"/>
</RO_NUMBER>
<PLANT>
<xsl:value-of select="PLANT"/>
</PLANT>
<ALLOCATION_AMOUNT>
<xsl:value-of select="ALLOCATION_AMOUNT"/>
</ALLOCATION_AMOUNT>
</RecordLine>
</xsl:if>
</xsl:for-each>
</ns0:MT_ActualCostPreNormalized>
</xsl:template>
</xsl:stylesheet>
[1]: http://i.stack.imgur.com/yklFq.jpg
在实施Daniel Haley提供的大部分代码之后,似乎解决方案非常接近。 Follwoing Martin的请求我在这里添加当前使用的XSLT +当前结果XML。 我将尝试并遵守我的XSLT计划规则: 1.源XML的每条记录应分析如下: 如果LINE_NUMBER = 0那么这是一个标题记录,应该产生一个段**。(它不应该被添加到段中)** - 如果LINE_NUMBER是&gt; 0和它的EVENT_ID等于产生段的行,这是同一段上下文中的RecordLine,并且应该在相同的段下打开一个段。 结果xml中可以看到很少的问题:
答案 0 :(得分:1)
当你写下你想要分组元素时,我想作为一个起点,你可以使用for-each-group
而不是for-each
:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:ns0="http://percite:scmaster/actual_costs">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<ns0:MT_ActualCostPreNormalized>
<xsl:for-each-group select="row" group-by="EVENT_ID">
<Header>
<xsl:apply-templates select="EVENT_ID, LINE_NUMBER, current-group()"/>
</Header>
</xsl:for-each-group>
</ns0:MT_ActualCostPreNormalized>
</xsl:template>
<xsl:template match="row">
<RecordLine>
<xsl:apply-templates/>
</RecordLine>
</xsl:template>
</xsl:transform>
您可以在Header
中的apply-templates
下方列出您想要的其他元素。
答案 1 :(得分:0)
由于您使用的是XSLT 2.0,因此可以使用xsl:for-each-group
(see XSLT 2.0 grouping)按EVENT_ID
进行分组。
另外,使用identity transform来处理所有不需要更改的元素。
示例...
XML输入
<ns:MT_ActualCosts xmlns:ns="http://percite:scmaster/actual_costs">
<row>
<EVENT_ID>106</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>9999</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-09 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>0001</TOTAL_AMOUNT_NET>
<RO_NUMBER>102808</RO_NUMBER>
</row>
<row>
<EVENT_ID>106</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24444</INVOICE_NUMBER>
<PLANT>0003</PLANT>
<ALLOCATION_AMOUNT>122</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>109</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24458</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-09 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>0011</TOTAL_AMOUNT_NET>
<RO_NUMBER>102813</RO_NUMBER>
</row>
<row>
<EVENT_ID>109</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24458</INVOICE_NUMBER>
<PLANT>0003</PLANT>
<ALLOCATION_AMOUNT>11.1</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>108</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24535</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-19 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>11</TOTAL_AMOUNT_NET>
<RO_NUMBER>102811</RO_NUMBER>
</row>
<row>
<EVENT_ID>108</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24535</INVOICE_NUMBER>
<PLANT>0002</PLANT>
<ALLOCATION_AMOUNT>11</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-28 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>999999</TOTAL_AMOUNT_NET>
<RO_NUMBER>103063</RO_NUMBER>
</row>
<row>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<PLANT>0001</PLANT>
<ALLOCATION_AMOUNT>11.47</ALLOCATION_AMOUNT>
</row>
<row>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>2</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<PLANT>0001</PLANT>
<ALLOCATION_AMOUNT>11.53</ALLOCATION_AMOUNT>
</row>
</ns:MT_ActualCosts>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://percite:scmaster/actual_costs">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Identity transform.-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<ns:MT_ActualCostPreNormalized>
<!--Group rows by EVENT_ID.-->
<xsl:for-each-group select="row" group-by="EVENT_ID">
<!--Optional sort by EVENT_ID.-->
<xsl:sort select="current-grouping-key()"/>
<Header>
<!--Apply templates to the children of row's with a LINE_NUMBER of 0
followed by row's that have a LINE_NUMBER that isn't equal to 0.-->
<xsl:apply-templates select="current-group()[LINE_NUMBER=0]/*,
current-group()[not(LINE_NUMBER=0)]">
<!--Optional sort by LINE_NUMBER.-->
<xsl:sort select="LINE_NUMBER"/>
</xsl:apply-templates>
</Header>
</xsl:for-each-group>
</ns:MT_ActualCostPreNormalized>
</xsl:template>
<!--Because of the apply-templates, the only row elements matched
should be row's with a LINE_NUMBER > 0.-->
<xsl:template match="row">
<RecordLine>
<xsl:apply-templates/>
</RecordLine>
</xsl:template>
</xsl:stylesheet>
XML输出
<ns:MT_ActualCostPreNormalized xmlns:ns="http://percite:scmaster/actual_costs">
<Header>
<EVENT_ID>106</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>9999</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-09 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>0001</TOTAL_AMOUNT_NET>
<RO_NUMBER>102808</RO_NUMBER>
<RecordLine>
<EVENT_ID>106</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24444</INVOICE_NUMBER>
<PLANT>0003</PLANT>
<ALLOCATION_AMOUNT>122</ALLOCATION_AMOUNT>
</RecordLine>
</Header>
<Header>
<EVENT_ID>108</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24535</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-19 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>11</TOTAL_AMOUNT_NET>
<RO_NUMBER>102811</RO_NUMBER>
<RecordLine>
<EVENT_ID>108</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24535</INVOICE_NUMBER>
<PLANT>0002</PLANT>
<ALLOCATION_AMOUNT>11</ALLOCATION_AMOUNT>
</RecordLine>
</Header>
<Header>
<EVENT_ID>109</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24458</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-09 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>0011</TOTAL_AMOUNT_NET>
<RO_NUMBER>102813</RO_NUMBER>
<RecordLine>
<EVENT_ID>109</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24458</INVOICE_NUMBER>
<PLANT>0003</PLANT>
<ALLOCATION_AMOUNT>11.1</ALLOCATION_AMOUNT>
</RecordLine>
</Header>
<Header>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>0</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<INVOICE_DATE>2015-02-28 00:00:00.0</INVOICE_DATE>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<TOTAL_AMOUNT_NET>999999</TOTAL_AMOUNT_NET>
<RO_NUMBER>103063</RO_NUMBER>
<RecordLine>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>1</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<PLANT>0001</PLANT>
<ALLOCATION_AMOUNT>11.47</ALLOCATION_AMOUNT>
</RecordLine>
<RecordLine>
<EVENT_ID>171</EVENT_ID>
<LINE_NUMBER>2</LINE_NUMBER>
<INVOICE_NUMBER>24645</INVOICE_NUMBER>
<PLANT>0001</PLANT>
<ALLOCATION_AMOUNT>11.53</ALLOCATION_AMOUNT>
</RecordLine>
</Header>
</ns:MT_ActualCostPreNormalized>