XML Path(XPath)条件专门用于非数字字符

时间:2017-05-14 19:17:22

标签: xml xpath conditional-statements

我目前正在SAP PI(接收方确定对象)中构建一个XPath条件,它应该将消息路由到接收方1或接收方2.

业务发送的给定documentID值如下。

接收方1接收以下documentID范围内的消息 范围:“F00”到“F99”

接收方2接收以下documentID范围内的消息 范围:“FA0”到“FZ9”

我能想到的示例条件,但不确定这是否有效或逻辑是否正确。跟进问题,是否大于/小于符号接受非数字字符?

Receiver 1的条件(/ p1:Upload / ContainerEvent [WorkAssignmentID> = F00] EX AND / p1:Upload / ContainerEvent [WorkAssignmentID< = F99] EX)

接收者2的条件 (/ p1:Upload / ContainerEvent [WorkAssignmentID> = FA0] EX AND / p1:Upload / ContainerEvent [WorkAssignmentID< = FZ9] EX)

*我也在考虑是否可以在XPath中使用substring。随意提供您的意见。感谢

此致 查尔斯谭

2 个答案:

答案 0 :(得分:2)

Pure XPath 1.0解决方案:

  

Receiver 1接收以下documentID范围内的消息范围:F00   到F99

/*/Upload/ContainerEvent
               [WorkAssignmentId
                 [string-length() = 3]
                 [starts-with(., 'F')][substring(.,2,2) >= 0][99 >= substring(.,2,2)]
                ]/EX
  

Receiver 2接收以下documentID范围内的消息范围:FA0   到FZ9

/*/Upload/ContainerEvent
               [WorkAssignmentId
                   [string-length() = 3]
                   [starts-with(., 'F')] 
                   [26 > string-length(
                            translate('ABCDEFGHIJKLMNOPQRSTUVWXYZ',substring(.,2,1), ''))]
                   [substring(.,3,1) >= 0][9 >= substring(.,3,1)]
                ]/EX

以下是基于XSLT的验证

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

  <xsl:template match="/">
    <xsl:copy-of select=
    "/*/Upload/ContainerEvent
               [WorkAssignmentId
                 [string-length() = 3]
                 [starts-with(., 'F')][substring(.,2,2) >= 0][99 >= substring(.,2,2)]
                ]/EX"/>
==============================
    <xsl:copy-of select=
    "/*/Upload/ContainerEvent
               [WorkAssignmentId
                   [string-length() = 3]
                   [starts-with(., 'F')] 
                   [26 > string-length(
                            translate('ABCDEFGHIJKLMNOPQRSTUVWXYZ',substring(.,2,1), ''))]
                   [substring(.,3,1) >= 0][9 >= substring(.,3,1)]
                ]/EX"/>

  </xsl:template>
</xsl:stylesheet>

对以下XML源文档应用此转换时(未提供):

<p1>
    <Upload>
        <ContainerEvent>
            <WorkAssignmentId>F13</WorkAssignmentId>
            <EX>F13</EX>
        </ContainerEvent>
        <ContainerEvent>
            <WorkAssignmentId>F99</WorkAssignmentId>
            <EX>F99</EX>
        </ContainerEvent>
        <ContainerEvent>
            <WorkAssignmentId>E15</WorkAssignmentId>
            <EX>E15</EX>
        </ContainerEvent>
        <ContainerEvent>
            <WorkAssignmentId>FA7</WorkAssignmentId>
            <EX>FA7</EX>
        </ContainerEvent>
        <ContainerEvent>
            <WorkAssignmentId>FZ9</WorkAssignmentId>
            <EX>FZ9</EX>
        </ContainerEvent>
        <ContainerEvent>
            <WorkAssignmentId>FAB</WorkAssignmentId>
            <EX>FAB</EX>
        </ContainerEvent>
    </Upload>
</p1>

生成了想要的结果

<EX>F13</EX>
<EX>F99</EX>
==============================
<EX>FA7</EX>
<EX>FZ9</EX>

答案 1 :(得分:0)

回答你的后续问题(“大于/小于标志是否接受非数字字符?”):在XPath 1.0中,不,大于/小于仅在数字上运行。这在XPath 2.0中发生了变化。