使用XSLT 3.0将JSON转换为XML-元素键中的转义与号 [英] Convert JSON to XML using XSLT 3.0 - escape ampersand in element key
问题描述
Usign XSLT我们希望将JSON转换为XML:
Usign XSLT we wish to transform the JSON to XML :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs math" version="3.0">
<xsl:output indent="yes" />
<xsl:template match="data">
<xsl:copy-of select="json-to-xml(.)" />
</xsl:template>
</xsl:stylesheet>
我们从Microsoft Dynamics 365收集了以下JSON输出,希望将其转换为XML.
We have follwing JSON output from Microsoft Dynamics 365 that we wish to convert to XML.
JSON:
<data>
{
"@odata.context": "https:\/\/xxx.api.crm4.dynamics.com\/api\/data\/v9.0\/$metadata#msdyn_bookingjournals(msdyn_journaltype,msdyn_name,msdyn_starttime,msdyn_endtime,msdyn_duration,foc_sapstatisticalkeyfigurecod,statecode)",
"value": [
{
"@odata.etag": "W\/\"3935842\"",
"msdyn_journaltype@OData.Community.Display.V1.FormattedValue": "Travel",
"msdyn_journaltype": 690970002,
"msdyn_name": "Wartung - Wartung - 101 Innener Verkehrsring\/D\u00fcsseldorferstr ",
"msdyn_starttime@OData.Community.Display.V1.FormattedValue": "13.11.2018 13:11",
"msdyn_starttime": "2018-11-13T13:11:58Z",
"msdyn_endtime@OData.Community.Display.V1.FormattedValue": "13.11.2018 13:15",
"msdyn_endtime": "2018-11-13T13:15:58Z",
"msdyn_duration@OData.Community.Display.V1.FormattedValue": "4",
"msdyn_duration": 4,
"foc_sapstatisticalkeyfigurecod": "",
"statecode@OData.Community.Display.V1.FormattedValue": "Active",
"statecode": 0,
"msdyn_bookingjournalid": "cb6d62ee-49e7-e811-a958-000d3a29fea4"
},
{
"@odata.etag": "W\/\"3935846\"",
"msdyn_journaltype@OData.Community.Display.V1.FormattedValue": "Working Hours",
"msdyn_journaltype": 690970000,
"msdyn_name": "Wartung - Wartung - 101 Innener Verkehrsring\/D\u00fcsseldorferstr ",
"msdyn_starttime@OData.Community.Display.V1.FormattedValue": "13.11.2018 13:15",
"msdyn_starttime": "2018-11-13T13:15:58Z",
"msdyn_endtime@OData.Community.Display.V1.FormattedValue": "13.11.2018 13:20",
"msdyn_endtime": "2018-11-13T13:20:57Z",
"msdyn_duration@OData.Community.Display.V1.FormattedValue": "4",
"msdyn_duration": 4,
"foc_sapstatisticalkeyfigurecod": "",
"statecode@OData.Community.Display.V1.FormattedValue": "Active",
"statecode": 0,
"msdyn_bookingjournalid": "fe6d62ee-49e7-e811-a958-000d3a29fea4"
}
],
"@odata.nextLink": "https:\/\/xxx.api.crm4.dynamics.com\/api\/data\/v9.0\/bookableresourcebookings(b0fe5834-45e7-e811-a958-000d3a29fb7a)\/msdyn_bookableresourcebooking_msdyn_bookingjournal_Booking?$select=msdyn_journaltype,msdyn_name,msdyn_starttime,msdyn_endtime,msdyn_duration,foc_sapstatisticalkeyfigurecod,statecode&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%2520parentEntityId%253d%2522b0fe5834-45e7-e811-a958-000d3a29fb7a%2522%2520parentAttributeName%253d%2522msdyn_booking%2522%2520parentEntityObjectTypeCode%253d%25221145%2522%253e%253cmsdyn_bookingjournalid%2520last%253d%2522%257bFE6D62EE-49E7-E811-A958-000D3A29FEA4%257d%2522%2520first%253d%2522%257bCB6D62EE-49E7-E811-A958-000D3A29FEA4%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20\/%3E"
}
</data>
在> https://www.freeformatter.com/xsl-transformer上测试此XSLT转换时.html ,出现以下错误
Unable to generate the XML document using the provided XML/XSL input.
org.xml.sax.SAXParseException; lineNumber: 38; columnNumber: 325;
The entity name must immediately follow the '&' in the entity reference.
You most likely forgot to escape '&' into '&'
显然,这是由于状态码后的未转义的&符号所致
Apparently this is due to the unescaped ampersand after statecode in
"@odata.nextLink":"...foc_sapstatisticalkeyfigurecod,statecode&$skiptoken="
在替换&时,转换工作正常的转义值:
Conversion works fine when replacing the & with the escaped value :
"@odata.nextLink":"...foc_sapstatisticalkeyfigurecod,statecode&$skiptoken="
在从JSON到XML的XSLT转换中,是否有一种方法可以转义禁止的XML字符?
Is there a way to escape forbidden XML characters in the XSLT conversion from JSON to XML?
推荐答案
没有XSLT版本4.0.
There is no XSLT version 4.0.
在处理链的某个阶段,您将一些JSON(或类似JSON)的文本包装在<data>...</data>
标记中,而没有确保确保正确地转义了JSON文本中的特殊字符.
At some stage in your processing chain, you wrapped some JSON (or JSON-like) text in <data>...</data>
tags without taking care to ensure that special characters in the JSON text were properly escaped.
实际上,不需要将JSON封装在XML包装器中.您可以使用json-to-xml(unparsed-text('input.json'))
直接读取JSON输入文件.然后,您通常将通过执行命名模板(称为"main"或"xsl:initial-template")开始转换,而不是通过提供源XML文档并使用match="/"
来开始转换.
In fact, wrapping the JSON in an XML wrapper isn't necessary. You can read the JSON input file directly using json-to-xml(unparsed-text('input.json'))
. You would then typically start the transformation by executing a named template (called, say, 'main' or 'xsl:initial-template') rather than by supplying a source XML document and using match="/"
.
这篇关于使用XSLT 3.0将JSON转换为XML-元素键中的转义与号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!