使用simple transformation查找xml file内某个节点的attribute是否存在指定value

2846 篇文章 296 订阅

Created by Jerry Wang on Jun 05, 2014

下列report实现通过simple transformation查找xml 文件内下列路径的节点ds其attribute uri的值是否等于指定值:
clipboard2

REPORT zdoc_trans_find_namespace.
DATA: lv_xml     TYPE string,
      lv_xml2    TYPE string,
      lv_result1 TYPE abap_bool,
      lv_result2 TYPE abap_bool.
START-OF-SELECTION.
  lv_xml = '<?xml version="1.0" encoding="UTF-8"?>' &&
   `<ds:datastoreItem xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml" ds:itemID="{0090FA0D-8DC2-1ED3-B783-90F3808D030B}">`
   && `<ds:schemaRefs><ds:schemaRef ds:uri="http://schemas.sap.com/crm"/></ds:schemaRefs></ds:datastoreItem>`.
  lv_xml2 = zcl_jerry_tool=>get_file_content_by_path( '\\TSHomeServer\TSHome$\i042416\Desktop\1.xml' ).
  CALL TRANSFORMATION zcontains_customxml
                   PARAMETERS my_namespace = 'http://schemas.sap.com/crm'
                   SOURCE XML lv_xml
                   RESULT result = lv_result1.
  CALL TRANSFORMATION zcontains_customxml
                  PARAMETERS my_namespace = 'http://schemas.sap.com/crm'
                  SOURCE XML lv_xml2
                  RESULT result = lv_result2.
  WRITE:/ 'Result1: ', lv_result1, ' Result2: ' , lv_result2.

使用tcode STRANS创建simple transformation,copy如下source code:

<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:sap="http://www.sap.com/sapxsl"
  xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml"
>
<xsl:param name="MY_NAMESPACE" sap:type="string" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
  <asx:values>
      <xsl:apply-templates select="ds:datastoreItem/ds:schemaRefs/ds:schemaRef"/>
  </asx:values>
</asx:abap>
</xsl:template>
<xsl:template match="ds:datastoreItem/ds:schemaRefs/ds:schemaRef">
  <xsl:if test="@ds:uri = $MY_NAMESPACE">
  <RESULT>X</RESULT>
  </xsl:if>
</xsl:template>
</xsl:transform>

运行结果:
clipboard3

关于xslt的语法

  1. The xsl:template element contains rules to apply when a specified node is matched.

The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).
2. The xsl:apply-templates element applies a template to the current element or to the current element’s child nodes.
If we add a select attribute to the xsl:apply-templates element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汪子熙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值