XPATH:for-each中的多个否定

时间:2012-03-21 10:33:31

标签: xslt xpath foreach

我有以下简化的XML:

<?xml version="1.0" encoding="UTF-8" ?>
<MATMAS05>
<IDOC BEGIN="1">
    <E1MARAM SEGMENT="1">
        <MSGFN>005</MSGFN>
        <MATNR>000000000000401436</MATNR>
        <E1MARCM SEGMENT="1">
            <MSGFN>005</MSGFN>
            <WERKS>A120</WERKS>
            <MMSTA>01</MMSTA>
        </E1MARCM>
        <E1MVKEM SEGMENT="1">
            <VKORG>0120</VKORG>
            <VMSTA>04</VMSTA>
        </E1MVKEM>
    </E1MARAM>
</IDOC>
</MATMAS05>

如果<WERKS>=A120<MMSTA> is NOT '01' or '02' or '03'<VKORG>=0120<VMSTA> is NOT '01' or '02' or '03',那么<MATNR>应该映射到目标XML。

我提出了以下XSLT:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" method="xml" indent="yes"/>

<xsl:template match="/*">
    <xsl:for-each
        select="IDOC[(E1MARAM/E1MVKEM[VKORG='0120'][not(VMSTA='01' or VMSTA='02' or VMSTA='03')])
            or (E1MARAM/E1MARCM[WERKS = 'A120'][not(MMSTA='01' or MMSTA='02' or MMSTA='03')])]">
        <Item>
            <ITEM_CODE>
                <xsl:value-of select="E1MARAM/MATNR"/>
            </ITEM_CODE>
        </Item>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

但如果我应用XSLT,我会得到以下不需要的输出(因为<MMSTA>='01'):

<?xml version="1.0" encoding="UTF-8"?>
<Item>
<ITEM_CODE>000000000000401436</ITEM_CODE>
</Item>

我该如何解决这个问题?我已尝试过XPATH表达式,但我无法得到想要的结果。我在XPATH中做错了什么?

感谢您对此提出的任何想法。 最好的祝福, 彼得

3 个答案:

答案 0 :(得分:1)

not(MMSTA='01' or MMSTA='02' or MMSTA='03')返回true总是因为.. MMSTA不是IDOC的孩子所以你应该使用//MMSTA

答案 1 :(得分:1)

我想出了这个:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output  method="xml" indent="yes"/>

<xsl:template match="/*">
    <xsl:for-each
        select="IDOC[(E1MARAM/E1MARCM/WERKS/text() = 'A120' 
and descendant::MMSTA/text() != '01' 
and descendant::MMSTA/text() != '02' 
and descendant::MMSTA/text() != '03')
or (E1MARAM/E1MVKEM/VKORG/text() = '120' 
and descendant::VMSTA/text() != '01'
and descendant::VMSTA/text() != '02'
and descendant::VMSTA/text() != '03')]">
        <Item>
            <ITEM_CODE>
                <xsl:value-of select="E1MARAM/MATNR"/>
            </ITEM_CODE>
        </Item>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

答案 2 :(得分:1)

这就是我所做的:

//IDOC[(E1MARAM/E1MVKEM[VKORG='0120' and not(VMSTA='01' or VMSTA='02' or VMSTA='03')]) or (E1MARAM/E1MARCM[WERKS='A120' and not(MMSTA='01' or MMSTA='02' or MMSTA='03')])]

我也尝试了你的,如果你将根添加到IDOC,它就有效,因为MATMAS05是实际的根:

//IDOC[(E1MARAM/E1MVKEM[VKORG='0120'][not(VMSTA='01' or VMSTA='02' or VMSTA='03')])             or (E1MARAM/E1MARCM[WERKS = 'A120'][not(MMSTA='01' or MMSTA='02' or MMSTA='03')])]