Created by Jerry Wang on Jun 05, 2014
下列report实现通过simple transformation查找xml 文件内下列路径的节点ds其attribute uri的值是否等于指定值:
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>
运行结果:
关于xslt的语法
- 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.