0

我有此要求将 DNR2(父)消息数据中的数据连接到 DNRL(子)消息数据中

这是我的源 XML:

 <?xml version="1.0" encoding="UTF-8"?>
<ns1:Delivery2 xmlns:ns1="http://www.norskeskog.com/MES">
  <ns1:FGOD>
    <ns1:DNH2>
      <ns1:OIT_ID>1025</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNH2</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>0000000000000019158100000000106448              00422  DE22ZCC2110600000027032500000227032502CC20DC          SUDU 198007-0                           .054349             2002432011081008150520110810                                                                                                                              delivery note                                                         0010002                                        FR. MEYERS SOHN GMBH&amp;CO                 20110820</ns1:MESSAGE_DATA>
    </ns1:DNH2>
    <ns1:DNR2>
      <ns1:OIT_ID>1026</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNR2</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000022000000270325000002201108084325164108562025I2                              0006               10000444158000000000000856702000600</ns1:MESSAGE_DATA>
    </ns1:DNR2>
    <ns1:DNRL>
      <ns1:OIT_ID>1027</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNRL</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000043251641085620254325164104282025</ns1:MESSAGE_DATA>
    </ns1:DNRL>
    <ns1:DNRL>
      <ns1:OIT_ID>1028</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNRL</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000043251641085620254325174104282025</ns1:MESSAGE_DATA>
    </ns1:DNRL>
    <ns1:DNR2>
      <ns1:OIT_ID>1029</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNR2</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000022000000270325000002201108084325164208602025I2                              0006               10000444158000000000000860702000600</ns1:MESSAGE_DATA>
    </ns1:DNR2>
    <ns1:DNRL>
      <ns1:OIT_ID>1030</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNRL</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
<ns1:MESSAGE_DATA>000000000000001915810000000043251642086020254325164204302025</ns1:MESSAGE_DATA>
    </ns1:DNRL>
  </ns1:FGOD>
</ns1:Delivery2>

我只需要将 DNR2 的消息数据连接到 DNRL 中,请记住 DNR2 是基于消息数据中的某些字符串的 DNRL 的父级。

我试过这样做,但是生成的 XML 包含重复的消息数据,因为它为每个 DNR2 迭代

所需目标:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:Delivery2 xmlns:ns1="http://www.norskeskog.com/MES">
  <ns1:FGOD>
    <ns1:DNH2>
      <ns1:OIT_ID>1025</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNH2</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>0000000000000019158100000000106448              00422  DE22ZCC2110600000027032500000227032502CC20DC          SUDU 198007-0                           .054349             2002432011081008150520110810                                                                                                                              delivery note                                                         0010002                                        FR. MEYERS SOHN GMBH&amp;CO                 20110820</ns1:MESSAGE_DATA>
    </ns1:DNH2>
    <ns1:DNR2>
      <ns1:OIT_ID>1026</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNR2</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000022000000270325000002201108084325164108562025I2                              0006               10000444158000000000000856702000600</ns1:MESSAGE_DATA>
    </ns1:DNR2>
    <ns1:DNRL>
      <ns1:OIT_ID>1027</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNRL</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000043251641085620254325164104282025 000000000000001915810000000022000000270325000002201108084325164108562025I2                              0006               10000444158000000000000856702000600</ns1:MESSAGE_DATA>
    </ns1:DNRL>
    <ns1:DNRL>
      <ns1:OIT_ID>1028</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNRL</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000043251641085620254325174104282025 000000000000001915810000000022000000270325000002201108084325164108562025I2                              0006               10000444158000000000000856702000600</ns1:MESSAGE_DATA>
    </ns1:DNRL>
    <ns1:DNR2>
      <ns1:OIT_ID>1029</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNR2</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000022000000270325000002201108084325164208602025I2                              0006               10000444158000000000000860702000600</ns1:MESSAGE_DATA>
    </ns1:DNR2>
    <ns1:DNRL>
      <ns1:OIT_ID>1030</ns1:OIT_ID>
      <ns1:CONTEXT>FGOD</ns1:CONTEXT>
      <ns1:MESSAGE_ID>DNRL</ns1:MESSAGE_ID>
      <ns1:VERSION>40B</ns1:VERSION>
      <ns1:SOURCE_SYS>MWWA</ns1:SOURCE_SYS>
      <ns1:DESTIN_SYS>SAPT</ns1:DESTIN_SYS>
      <ns1:TIME_STAMP>2012-06-18T12:55:00</ns1:TIME_STAMP>
      <ns1:FUNCTION>NEW</ns1:FUNCTION>
      <ns1:PRIORITY>
      </ns1:PRIORITY>
      <ns1:STATUS>1</ns1:STATUS>
      <ns1:SAP_IDOC_NUMBER/>
      <ns1:MESSAGE_DATA>000000000000001915810000000043251642086020254325164204302025 000000000000001915810000000022000000270325000002201108084325164208602025I2                              0006               10000444158000000000000860702000600</ns1:MESSAGE_DATA>
    </ns1:DNRL> 
  </ns1:FGOD>
</ns1:Delivery2>

我的尝试:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://www.norskeskog.com/MES" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="ns0 xs">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <Delivery2 xmlns="http://www.norskeskog.com/MES">
            <xsl:attribute name="xsi:schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance">http://www.norskeskog.com/MES C:/Users/scarlosb/Desktop/DEV/FGOD.xsd</xsl:attribute>
            <xsl:for-each select="ns0:Delivery2">
                <xsl:variable name="var2_cur" select="."/>
                <FGOD>
                    <DNH2>
                        <xsl:copy-of select="ns0:FGOD/ns0:DNH2/@node()"/>
                        <xsl:copy-of select="ns0:FGOD/ns0:DNH2/node()"/>
                    </DNH2>
                    <xsl:for-each select="ns0:FGOD/ns0:DNR2">
                        <DNR2>
                            <xsl:copy-of select="@node()"/>
                            <xsl:copy-of select="node()"/>
                        </DNR2>
                    </xsl:for-each>
                    <xsl:for-each select="ns0:FGOD/ns0:DNRL">
                        <xsl:variable name="var1_cur" select="."/>
                        <DNRL>
                            <OIT_ID>
                                <xsl:value-of select="string(floor(number(string(ns0:OIT_ID))))"/>
                            </OIT_ID>
                            <CONTEXT>
                                <xsl:value-of select="string(ns0:CONTEXT)"/>
                            </CONTEXT>
                            <MESSAGE_ID>
                                <xsl:value-of select="string(ns0:MESSAGE_ID)"/>
                            </MESSAGE_ID>
                            <VERSION>
                                <xsl:value-of select="string(ns0:VERSION)"/>
                            </VERSION>
                            <SOURCE_SYS>
                                <xsl:value-of select="string(ns0:SOURCE_SYS)"/>
                            </SOURCE_SYS>
                            <DESTIN_SYS>
                                <xsl:value-of select="string(ns0:DESTIN_SYS)"/>
                            </DESTIN_SYS>
                            <TIME_STAMP>
                                <xsl:value-of select="string(ns0:TIME_STAMP)"/>
                            </TIME_STAMP>
                            <FUNCTION>
                                <xsl:value-of select="string(ns0:FUNCTION)"/>
                            </FUNCTION>
                            <PRIORITY>
                                <xsl:value-of select="string(ns0:PRIORITY)"/>
                            </PRIORITY>
                            <STATUS>
                                <xsl:value-of select="string(floor(number(string(ns0:STATUS))))"/>
                            </STATUS>
                            <SAP_IDOC_NUMBER>
                                <xsl:value-of select="/.."/>
                            </SAP_IDOC_NUMBER>
                            <xsl:for-each select="$var2_cur/ns0:FGOD/ns0:DNR2">
                                <MESSAGE_DATA>
                                    <xsl:value-of select="concat(string($var1_cur/ns0:MESSAGE_DATA), string(ns0:MESSAGE_DATA))"/>
                                </MESSAGE_DATA>
                            </xsl:for-each>
                        </DNRL>
                    </xsl:for-each>
                </FGOD>
            </xsl:for-each>
        </Delivery2>
    </xsl:template>
</xsl:stylesheet>
4

1 回答 1

1

这个 XSLT 1.0 样式表应该可以解决问题。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns1="http://www.norskeskog.com/MES">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
 <xsl:copy> 
   <xsl:apply-templates select="@*|node()" /> 
 </xsl:copy> 
</xsl:template>  

<xsl:template match="ns1:DNRL/ns1:MESSAGE_DATA">
 <xsl:copy> 
   <xsl:apply-templates select="@*" /> 
   <xsl:value-of select="concat(., ' ', ../preceding-sibling::ns1:DNR2[1]/ns1:MESSAGE_DATA)" /> 
   <xsl:apply-templates select="*" /> 
 </xsl:copy> 
</xsl:template>  

</xsl:stylesheet>  

笔记

  1. 使用身份模板的强大功能可以为您节省大量的脚本编写工作。
  2. 编写模板来处理来自身份转换的所需变化。
于 2012-07-08T16:51:14.817 回答