在标签<PACKAGE>取消合并</PACKAGE>下选择一个字符串

时间:2019-10-31 10:22:21

标签: regex

我有以下代码:

<TASKDATA>
<PACKAGE>BADI BS Aggr Profit Centers to Entity</PACKAGE>
<TEAM/>
<GROUP>Financial Process</GROUP>
<APPSET>OneMIS_DEV</APPSET>
<APPLICATION>Planning_DEV</APPLICATION>
<ANSWERPROMPT>%SELECTION% 
|DIMENSION:CATEGORY|AC0|DIMENSION:P_ENTITY||10DIMENSION:TIME|2019.09
%SELECTION_KEYDATE% </ANSWERPROMPT>
</TASKDATA>
</LINK_PROCESS>
<LINK_PROCESS LINK_TYPE="BPC_PACKAGE">
<PROPERTY NAME="NAME">Deconsolidation</PROPERTY>
<PROPERTY NAME="DESCRIPTION">Deconsolidation (Static Comp Code 
Filter)</PROPERTY>
<PROPERTY NAME="ID">583FA498C7BE45D4BECFF0F73FADA10B</PROPERTY>
<TASKDATA>
<PACKAGE>Deconsolidation</PACKAGE>
<TEAM/>
<GROUP>Financial Process</GROUP>
<APPSET>OneMIS_DEV</APPSET>
<APPLICATION>Planning_DEV</APPLICATION>
<ANSWERPROMPT>%SELECTION%
|DIMENSION:CATEGORY|AC0|DIMENSION:P_ENTITY|CZ20|27DIMENSION:TIME|2019.09 %SELECTION_KEYDATE% </ANSWERPROMPT>
</TASKDATA>

我只需要选择标签Deconsolidation下的字符串P_ENTITY。例如,在示例中,必须跳过第9行的字符串P_ENTITY,因为上面有标签BADI BS Aggr实体的利润中心。另一方面,必须选择第26行的字符串P_ENTITY,因为上面有标签Deconsolidation我该怎么办?。

我尝试使用正则表达式P_ENTITY.{1,1}([A-Z]{2}[0-9]{2},*)*,但它会选择所有P_ENTITY字符串。

1 个答案:

答案 0 :(得分:0)

假设您具有完全有效的XML,即您的XML包含<LINK_PROCESS>标签的父标签

<LINKS>
    <LINK_PROCESS LINK_TYPE="BPC_PACKAGE">
        <TASKDATA>
            <PACKAGE>BADI BS Aggr Profit Centers to Entity</PACKAGE>
            <TEAM/>
            <GROUP>Financial Process</GROUP>
            <APPSET>OneMIS_DEV</APPSET>
            <APPLICATION>Planning_DEV</APPLICATION>
            <ANSWERPROMPT>%SELECTION% |DIMENSION:CATEGORY|AC0|DIMENSION:P_ENTITY||10DIMENSION:TIME|2019.09%SELECTION_KEYDATE% </ANSWERPROMPT>
        </TASKDATA>
    </LINK_PROCESS>
    <LINK_PROCESS LINK_TYPE="BPC_PACKAGE">
        <PROPERTY NAME="NAME">Deconsolidation</PROPERTY>
        <PROPERTY NAME="DESCRIPTION">Deconsolidation (Static Comp Code Filter)</PROPERTY>
        <PROPERTY NAME="ID">583FA498C7BE45D4BECFF0F73FADA10B</PROPERTY>
        <TASKDATA>
            <PACKAGE>Deconsolidation</PACKAGE>
            <TEAM/>
            <GROUP>Financial Process</GROUP>
            <APPSET>OneMIS_DEV</APPSET>
            <APPLICATION>Planning_DEV</APPLICATION>
            <ANSWERPROMPT>%SELECTION%|DIMENSION:CATEGORY|AC0|DIMENSION:P_ENTITY|CZ20|27DIMENSION:TIME|2019.09 %SELECTION_KEYDATE% </ANSWERPROMPT>
        </TASKDATA>
    </LINK_PROCESS>
</LINKS>

您可以使用以下ST转换zdecon

来解析XML
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" template="main">
  <tt:root name="ROOT"/>
  <tt:template name="main">
   <LINKS>
    <tt:loop name="line" ref="ROOT">
      <LINK_PROCESS>
      <tt:skip name="PROPERTY" count="*" />
        <TASKDATA>
          <PACKAGE>
            <tt:value ref="package"/>
          </PACKAGE>
          <tt:skip count="4"/>
          <ANSWERPROMPT>
            <tt:value ref="answer"/>
          </ANSWERPROMPT>
        </TASKDATA>
      </LINK_PROCESS>
    </tt:loop>
    </LINKS>
  </tt:template>
</tt:transform>

并在ABAP中这样称呼它:

TYPES: BEGIN OF entity,
        package TYPE string,
        answer TYPE string,
      END OF entity,
      tt_entities TYPE STANDARD TABLE OF entity WITH DEFAULT KEY.

DATA: xml_string TYPE string.
DATA: tab        TYPE tt_entities.

xml_string = ` <LINKS><LINK_PROCESS LINK_TYPE="BPC_PACKAGE"> ` &&
` <TASKDATA> ` &&
...
`   </TASKDATA> ` &&
`   </LINK_PROCESS></LINKS>`.

CALL TRANSFORMATION zdecon
             SOURCE XML xml_string
             RESULT root = tab.

DELETE tab WHERE package <> 'Deconsolidation'.

LOOP AT tab ASSIGNING FIELD-SYMBOL(<line>).
  SPLIT <line>-answer AT '|' INTO TABLE DATA(deconsolidations).
  FIND REGEX '.+P_ENTITY' IN TABLE deconsolidations MATCH LINE DATA(l).
  WRITE: deconsolidations[ l + 1 ].
ENDLOOP.

为您的XML做出的几个假设:

  1. <PROPERTY>标签是任意的,可能会丢失

  2. <ANSWERPROMPT>标签包含%SELECTION%占位符包裹的尺寸列表

  3. 每个维度都以|DIMENSION:<dim_name>|<dim_value>|的形式出现

  4. DIMENSION关键字前的随机数无关紧要,可以忽略

27 维度:时间