word模板和XML数据源是如何合并生成最后的word文档的详细过程

简介: word模板和XML数据源是如何合并生成最后的word文档的详细过程

This document is used for consultant or developers at customer side who would like to know technical detail about how a word template is merged with xml data stream. Before you touch the related ABAP code about document merging, it is good to get some basic understandong about how ABAP manipulates word document in this document Manipulate Docx document with ABAP.


In the document Create Webservice enabled word document in attachment assignment block, it is described how to create a word template and feed its content with web service generated via web service tool in CRM Webclient UI. Besides that you could also use your own web service, for example you can expose your function module in ABAP backend via SOAMANAGER. The detailed step could be found in this document.


In consulting note 2048272, an ABAP report is provided to allow you to merge the word template with given xml data without Webclient UI:


image.png


If you meet with issue that the merged word document does not work as you expected, for example some fields are empty however the corresponding node in xml do have data filled, then you can use this ABAP report to do trouble shooting. You could get the word template and xml data file for web service response by following consulting note 2047259.


In this document, I will show you how the element “Partner” in word template is merged with the value from xml node SOLD_TO.PARTNER in the runtime. You could find the template “zpartner.docx” and xml data file “response.xml” used in this example from document attachment.


image.png


Explore the word template to identify binding information

From the above screenshot, we click on Partner field and the tooltip shows this element is bound to field PARTNER. How is this mapping relationship maintained within word template?


Rename zparetner.docx to zpartner.zip, double click on the zip file, double click subfolder “word”, then double click on “document.xml”:

You should find a w:t node with PARTNER, and a w:tag node with attribute “w:val”, whose value is 0050569457471ED488F2038D1D8360BE.

The w:t node contains the text which end user sees in the word template, and w:val maintains the mapping relationship to xml schema.


image.png


Now go back to zpartner.zip, click on customXml folder and double click “item1.xml”. Search xml file by value “0050569457471ED488F2038D1D8360BE”. One result found, and we could know the complete binding path is Z_JENS_GET_MORE_COMPLEXResponse.ES_C5064266.SOLD_TO.PARTNER.


image.png


Merge step1 cl_crm_oi_docx_helper=>replace_vars_uuid_with_path

In this step, the original uuid in document.xml is replaced by the complete path of bound xml node:




Merge step2 add necessary namespace to every node in xml

It is done via transformation call:image.png


image.pngimage.png

Notice the no namespace added for each node after transformation call:


image.png


Merge step3 cl_crm_oi_docx_transform_rt=>transform

the formatted template source( uuid replaced with complete path ) and formatted xml data source( n0 namespace added ) are passed into this method.


step3.1 cl_crm_oi_docx_transform_rt=>indicate_tree

The xml node is further enriched with attribute Loopcount and Path, which are especially used for render data with table structure. ( template merged with table data will be discussed in another document )


image.png


step3.2 cl_crm_oi_docx_transform_rt=>update_payload_from_addinsch

This method will create a new attribute sapvartype with value “T” for those node in xml source with table structure, not relevant for current example.


image.png


step3.3 recursively call cl_crm_oi_docx_transform_rt=>process_node_cc

node: dom element of root node “Z_JENS_GET_MORE_COMPLEXResponse” in xml.


image.png



Now we have reached the first leaf node in xml, “CLIENT”. Pay attention to the recursive callstack.


image.png


since we don’t bind CLIENT node in xml to our template, after method get_contentcontrol_node_bypath, lt_node_ref_word will remain empty so we quit from the current traverse and try with next node.



image.png

Soon we reach the method get_contentcontrol_node_bypath again, and since our template consumes the node PARTNER, it is ready to feed the template with the value contained in this xml node.


image.png


The sold to partner id “0000419333” will be set to the corresponding dom element node “lr_node_word” by the method below.


image.png


in the beginning of this document we know it is the content of w:t node which will be displayed to end user so first we have to locate those w:t nodes via filtering ( w:t node found and stored in variable node_text ) and finally fill value to them by method fill_text_node, and that’s all.


image.png


final merged document:


image.png


And you could find the w:t node contains the correct value from document source code:


image.png


Next reading

Understand how the word template is merged with xml data stream – part 2 – table structure


相关文章
|
9月前
|
XML JSON fastjson
企业微信通讯录回调模板-XML转JSON-让回调不再难!
企业微信通讯录回调模板-XML转JSON-让回调不再难!
352 0
|
XML 数据格式
hutool将XML文档转换为String
hutool将XML文档转换为String
|
16天前
|
XML JavaScript 前端开发
XML文档节点导航与选择指南
XPath是XSLT的核心部分,用于XML文档的节点定位和选择。它采用路径表达式语法,包含200多个内置函数处理各种数据类型。XPath在编程语言如JavaScript中广泛使用,与XSLT配合进行XML转换和样式处理。它涉及7种节点类型,如元素、属性和文本,以及多种节点间关系,如父、子、同级等。XPath还使用轴(如ancestor、child)来描述节点间的关联,并支持运算符进行逻辑和数学操作。
27 0
XML文档节点导航与选择指南
|
6月前
|
XML JavaScript 前端开发
SAP UI5 本地开发如何实现 XML 和 JavaScript 代码的自动完成和嵌入式 API 文档自动显示试读版
SAP UI5 本地开发如何实现 XML 和 JavaScript 代码的自动完成和嵌入式 API 文档自动显示试读版
65 0
|
4月前
|
Java
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
36 0
|
5月前
|
XML 数据格式
wps2013打开word是xml
wps2013打开word是xml
|
6月前
|
XML JavaScript 数据格式
使用Dom4J解析XML文档
XML解析的方式 XML常见的两种解析方式: DOM: 要求解析器将整个XML文件全部加载到内存中,生成一个Document对象 优点:元素和元素之间保留结构、关系,可以针对元素进行增删查改操作 缺点:如果XML文件过大,可能会导致内存溢出 SAX:是一种速度更快,更加高效的解析方式。它是逐行扫描,边扫描边解析,并且以事件驱动的方式来进行具体的解析,每解析一行都会触发一个事件 优点: 不会出现内存溢出的问题,可以处理大文件 缺点:只能读,不能写 概念辨析: 解析器就是根据不同的解析方式提供具体的实现。 为了方便开发人员来解析XML,有一些方便操作的类库。例如Dom4j其中就包含了很多解
58 0
|
6月前
|
XML JSON JavaScript
SAP Fiori Elements 应用里标准模板 XML Fragment 加载的逻辑和 XMLPreprocessor 的作用
SAP Fiori Elements 应用里标准模板 XML Fragment 加载的逻辑和 XMLPreprocessor 的作用
52 0
|
7月前
|
XML 数据格式
idea建xml模板
idea建xml模板
30 0
|
11月前
|
XML Android开发 数据格式
eclipse/MyEclipse代码模板XML配置文件分享,爽爆了~
eclipse/MyEclipse代码模板XML配置文件分享,爽爆了~