我在生成 XML 时遇到问题。我使用了简单转换。我的 XML 中的许多标签都是空的。我发现了一个信息,我可以使用 Regex 摆脱这些标签,但它不能完美地工作。让我向你展示它的外观。
没有正则表达式:
<?xml version="1.0" encoding="utf-8" ?>
<Invoice
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<cbc:DueDate />
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
<cbc:Note />
<cbc:DocumentCurrencyCode>PLN</cbc:DocumentCurrencyCode>
<cbc:TaxCurrencyCode />
<cbc:BuyerReference />
<cac:InvoicePeriod>
<cbc:StartDate />
<cbc:EndDate />
<cbc:DescriptionCode />
</cac:InvoicePeriod>
用 ABAP 编写的正则表达式:
REPLACE ALL OCCURRENCES OF REGEX
'(<!\[CDATA\[([^]]|(\][^]])|(\]\][^>]))*\]\]>)|(<([^?][^><\s]*)(\s[^><]+)?/>)'
IN exportxml
WITH '$1'.
使用正则表达式后:
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>PLN</cbc:DocumentCurrencyCode>
<cac:InvoicePeriod />
SimpleTransformation 看起来像这样:
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
<tt:root name="ZXT_INVOICE" type="ddic:ZXT_INVOICE"/>
<tt:template>
<Invoice
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:ccts="urn:un:unece:uncefact:documentation:2"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
>
<cbc:DueDate tt:value-ref=".ZXT_INVOICE.DUEDATE"/>
<cbc:InvoiceTypeCode tt:value-ref=".ZXT_INVOICE.INVOICETYPECODE"/>
<cbc:Note tt:value-ref=".ZXT_INVOICE.NOTE"/>
<cbc:DocumentCurrencyCode tt:value-ref=".ZXT_INVOICE.DOCUMENTCURRENCYCODE"/>
<cbc:TaxCurrencyCode tt:value-ref=".ZXT_INVOICE.TAXCURRENCYCODE"/>
<cbc:AccountingCost tt:value-ref=".ZXT_INVOICE.ACCOUNTINGCOST"/>
<cbc:BuyerReference tt:value-ref=".ZXT_INVOICE.BUYERREFERENCE"/>
<cac:InvoicePeriod>
<cbc:StartDate tt:value-ref=".ZXT_INVOICE.INVOICE_PERIOD.STARTDATE"/>
<cbc:EndDate tt:value-ref=".ZXT_INVOICE.INVOICE_PERIOD.ENDDATE"/>
<cbc:DescriptionCode tt:value-ref=".ZXT_INVOICE.INVOICE_PERIOD.DESCRIPTIONCODE"/>
</cac:InvoicePeriod>
</Invoice>
</tt:template>
</tt:transform>
正则表达式删除简单的元素,但有嵌套元素的问题,如<cac:InvoicePeriod>
. 在我的程序中,我有很多嵌套元素。你能帮我修改正则表达式或找到其他解决方案吗?
谢谢你的帮助。