我有一个与此类似的xml:
<?xml version="1.0"?>
<MATMAS05>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<CREDAT>20180822</CREDAT>
<CRETIM>180201</CRETIM>
<SERIAL>20180821134354</SERIAL>
</EDI_DC40>
<E1MARAM SEGMENT="1">
<MSGFN>005</MSGFN>
<MATNR>000000000002000010</MATNR>
<E1MARA1 SEGMENT="1">
<MAXC>0.000</MAXC>
<MAXC_TOL>0.0</MAXC_TOL>
</E1MARA1>
<E1MAKTM SEGMENT="1">
<MSGFN>005</MSGFN>
<SPRAS>E</SPRAS>
</E1MAKTM>
<E1MARCM SEGMENT="1">
<MSGFN>009</MSGFN>
<WERKS>3030</WERKS>
<E1MARDM SEGMENT="1">
<MSGFN>009</MSGFN>
<LGORT>1000</LGORT>
</E1MARDM>
<E1MPGDM SEGMENT="1">
<MSGFN>005</MSGFN>
</E1MPGDM>
</E1MARCM>
<E1MARCM SEGMENT="1">
<MSGFN>005</MSGFN>
<WERKS>3040</WERKS>
<E1MARDM SEGMENT="1">
<MSGFN>005</MSGFN>
<LGORT>1000</LGORT>
</E1MARDM>
<E1MPGDM SEGMENT="1">
<MSGFN>005</MSGFN>
</E1MPGDM>
</E1MARCM>
<E1MARMM SEGMENT="1">
<MSGFN>005</MSGFN>
<MEINH>EA</MEINH>
</E1MARMM>
<E1MBEWM SEGMENT="1">
<MSGFN>009</MSGFN>
<BWKEY>3030</BWKEY>
</E1MBEWM>
<E1MBEWM SEGMENT="1">
<MSGFN>005</MSGFN>
<BWKEY>3040</BWKEY>
</E1MBEWM>
<E1MLANM SEGMENT="1">
<MSGFN>005</MSGFN>
<ALAND>AU</ALAND>
</E1MLANM>
</E1MARAM>
</IDOC>
</MATMAS05>
我需要将E1MBEWM节点移动到BWKEY = WERKS的适当E1MARCM节点
因此,基本上应该是这样的输出:
<?xml version="1.0"?>
<MATMAS05>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<CREDAT>20180822</CREDAT>
<CRETIM>180201</CRETIM>
<SERIAL>20180821134354</SERIAL>
</EDI_DC40>
<E1MARAM SEGMENT="1">
<MSGFN>005</MSGFN>
<MATNR>000000000002000010</MATNR>
<E1MARA1 SEGMENT="1">
<MAXC>0.000</MAXC>
<MAXC_TOL>0.0</MAXC_TOL>
</E1MARA1>
<E1MAKTM SEGMENT="1">
<MSGFN>005</MSGFN>
<SPRAS>E</SPRAS>
</E1MAKTM>
<E1MARCM SEGMENT="1">
<MSGFN>009</MSGFN>
<WERKS>3030</WERKS>
<E1MARDM SEGMENT="1">
<MSGFN>009</MSGFN>
<LGORT>1000</LGORT>
</E1MARDM>
<E1MPGDM SEGMENT="1">
<MSGFN>005</MSGFN>
</E1MPGDM>
<E1MBEWM SEGMENT="1">
<MSGFN>009</MSGFN>
<BWKEY>3030</BWKEY>
</E1MBEWM>
</E1MARCM>
<E1MARCM SEGMENT="1">
<MSGFN>005</MSGFN>
<WERKS>3040</WERKS>
<E1MARDM SEGMENT="1">
<MSGFN>005</MSGFN>
<LGORT>1000</LGORT>
</E1MARDM>
<E1MPGDM SEGMENT="1">
<MSGFN>005</MSGFN>
</E1MPGDM>
<E1MBEWM SEGMENT="1">
<MSGFN>005</MSGFN>
<BWKEY>3040</BWKEY>
</E1MBEWM>
</E1MARCM>
<E1MARMM SEGMENT="1">
<MSGFN>005</MSGFN>
<MEINH>EA</MEINH>
</E1MARMM>
<E1MLANM SEGMENT="1">
<MSGFN>005</MSGFN>
<ALAND>AU</ALAND>
</E1MLANM>
</E1MARAM>
</IDOC>
</MATMAS05>
可能的问题是,有时E1MBEWM节点可能不在传入消息中。
我正在尝试使用XSLT之类的东西:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="E1MARCM/*[1]">
<xsl:copy-of select="/*/E1MBEWM[1]"/>
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="/*/E1MBEWM[1]"/>
</xsl:stylesheet>
,但如果有多个E1MARCM节点,则不会提供所需的结果。我了解每个周期需要使用两个,但不确定如何在此处实现。
可以帮忙吗?
答案 0 :(得分:0)
我的建议是XSL具有两个级别(两个模板):
E1MARCM
节点,并建立一个与匹配节点相等的节点。第二个节点,对于每个E1MARCM
节点,将复制一个满足要求条件的节点E1MBEWM
。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/XML">
<XML>
<xsl:apply-templates select="E1MARCM"/>
</XML>
</xsl:template>
<xsl:template match="E1MARCM">
<E1MARCM SEGMENT="{@SEGMENT}">
<MSGFN><xsl:value-of select="MSGFN"/></MSGFN>
<WERKS><xsl:value-of select="WERKS"/></WERKS>
<xsl:variable name="mywerks" select="WERKS"></xsl:variable>
<xsl:copy-of select="../E1MBEWM[BWKEY=$mywerks]"/>
</E1MARCM>
</xsl:template>
</xsl:stylesheet>
(请注意,在将某个节点的值放入上下文节点不同的xpath表达式之前,必须将其值存储在临时变量中。)
并且,为了概括属性和子节点,需要第三个目标:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/XML">
<XML>
<xsl:apply-templates select="E1MARCM"/>
</XML>
</xsl:template>
<xsl:template match="E1MARCM">
<E1MARCM>
<xsl:apply-templates select="@*" mode="just-copy"/>
<xsl:apply-templates select="*" mode="just-copy"/>
<xsl:variable name="mywerks" select="WERKS"></xsl:variable>
<xsl:copy-of select="../E1MBEWM[BWKEY=$mywerks]"/>
</E1MARCM>
</xsl:template>
<xsl:template match="*|@*" mode="just-copy">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
感谢小桑蒂。 我设法找到了适合自己目的的解决方案:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="E1MARCM">
<E1MARCM>
<xsl:apply-templates select="@*" mode="just-copy"/>
<xsl:apply-templates select="*" mode="just-copy"/>
<xsl:variable name="mywerks" select="WERKS"></xsl:variable>
<xsl:copy-of select="../E1MBEWM[BWKEY=$mywerks]"/>
</E1MARCM>
</xsl:template>
<xsl:template match="*|@*" mode="just-copy">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
我有一些重复,因为现在E1MARCM中有E1MBEWM了,但是我以后可以过滤掉它。