前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用simple transformation查找xml file内某个节点的attribute是否存在指定value

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

作者头像
Jerry Wang
发布2019-07-04 09:19:19
1K0
发布2019-07-04 09:19:19
举报

Created by Jerry Wang on Jun 05, 2014

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

clipboard2
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
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.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Created by Jerry Wang on Jun 05, 2014
  • 关于xslt的语法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档