如何通过 abap 中的 XSLT 程序读取 XML 文件的属性
how to read attributes of XML file via XSLT program in abap
我有一个如下所示的 XML 文件,我需要读取属性值并将其提取到内部 table。
但未获取值,内部 table 仍为空。如果代码有任何问题,请告诉我。提前致谢!
XML 文件:
<?xml version="1.0" encoding="iso-8859-1" ?>
<CUSTOMERS>
<PERSON customer_id="1" first_name="Jan" last_name="krohn">
</PERSON>
<PERSON customer_id="2" first_name="Jan1" last_name="krohn1">
</PERSON>
</CUSTOMERS>
xslt 程序:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<IPERSON>
<xsl:for-each select="/CUSTOMERS/PERSON">
<item>
<CUST_ID>
<xsl:value-of select="@customer_id"/>
</CUST_ID>
<FIRSTNAME>
<xsl:value-of select="first_name"/>
</FIRSTNAME>
<LASTNAME>
<xsl:value-of select="last_name"/>
</LASTNAME>
</item>
</xsl:for-each>
</IPERSON></asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>
abap 调用:
TYPES: BEGIN OF ts_person,
cust_id(4) TYPE c,
firstname(20) TYPE c,
lastname(20) TYPE c,
* ONE_STRING TYPE CHAR50,
END OF ts_person.
DATA : it_data TYPE STANDARD TABLE OF ts_person,
wa_data TYPE ts_person.
DATA: gt_result_xml TYPE abap_trans_resbind_tab,
gs_result_xml TYPE abap_trans_resbind.
GET REFERENCE OF it_data INTO gs_result_xml-value.
gs_result_xml-name = 'IPERSON'.
APPEND gs_result_xml TO gt_result_xml.
CALL TRANSFORMATION ZXSLT_1 " xslt file above
SOURCE XML it_xml
RESULT (gt_result_xml).
您缺少 @
属性名称的符号 first_name
和 last_name
<xsl:for-each select="/CUSTOMERS/PERSON">
<item>
<CUST_ID>
<xsl:value-of select="@customer_id" />
</CUST_ID>
<FIRSTNAME>
<xsl:value-of select="@first_name" />
</FIRSTNAME>
<LASTNAME>
<xsl:value-of select="@last_name" />
</LASTNAME>
</item>
</xsl:for-each>
我有一个如下所示的 XML 文件,我需要读取属性值并将其提取到内部 table。
但未获取值,内部 table 仍为空。如果代码有任何问题,请告诉我。提前致谢!
XML 文件:
<?xml version="1.0" encoding="iso-8859-1" ?>
<CUSTOMERS>
<PERSON customer_id="1" first_name="Jan" last_name="krohn">
</PERSON>
<PERSON customer_id="2" first_name="Jan1" last_name="krohn1">
</PERSON>
</CUSTOMERS>
xslt 程序:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<IPERSON>
<xsl:for-each select="/CUSTOMERS/PERSON">
<item>
<CUST_ID>
<xsl:value-of select="@customer_id"/>
</CUST_ID>
<FIRSTNAME>
<xsl:value-of select="first_name"/>
</FIRSTNAME>
<LASTNAME>
<xsl:value-of select="last_name"/>
</LASTNAME>
</item>
</xsl:for-each>
</IPERSON></asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>
abap 调用:
TYPES: BEGIN OF ts_person,
cust_id(4) TYPE c,
firstname(20) TYPE c,
lastname(20) TYPE c,
* ONE_STRING TYPE CHAR50,
END OF ts_person.
DATA : it_data TYPE STANDARD TABLE OF ts_person,
wa_data TYPE ts_person.
DATA: gt_result_xml TYPE abap_trans_resbind_tab,
gs_result_xml TYPE abap_trans_resbind.
GET REFERENCE OF it_data INTO gs_result_xml-value.
gs_result_xml-name = 'IPERSON'.
APPEND gs_result_xml TO gt_result_xml.
CALL TRANSFORMATION ZXSLT_1 " xslt file above
SOURCE XML it_xml
RESULT (gt_result_xml).
您缺少 @
属性名称的符号 first_name
和 last_name
<xsl:for-each select="/CUSTOMERS/PERSON">
<item>
<CUST_ID>
<xsl:value-of select="@customer_id" />
</CUST_ID>
<FIRSTNAME>
<xsl:value-of select="@first_name" />
</FIRSTNAME>
<LASTNAME>
<xsl:value-of select="@last_name" />
</LASTNAME>
</item>
</xsl:for-each>