gpt4 book ai didi

json - 通过 XSLT 将 XML 转换为 JSON

转载 作者:行者123 更新时间:2023-12-02 19:45:31 25 4
gpt4 key购买 nike

您能否提供将 XML 转换为 JSON 的 XSLT 代码?我是 SAP 开发人员,零基础
XSLT 方面的知识。 XSLT 必须能够将 XML 转换为 JSON 格式,如下所示。我需要将 XML 源转换为指定的 JSON 格式。为此,我需要删除 header 节点,保留数组主体并封装在 [ ] 中。我已经转换了主体,但在删除 header 节点和插入封装​​ [ ]

时遇到问题
This is the XML format I receive:

<?xml version="1.0" encoding="utf-8"?>
<n0:AA_JSON_Out xmlns:n0="http://PELASEHELP.com/PI/YYY" "namespace
xmlns:prx="urn:sap.com:proxy:AAA:/1SAI/TAS3DF8912DDF823A9E7198:750">
<root>
<header>
<id>GGG-00009</id>
</header>
<body>
<user_name>RAMBO</user_name>
<code>BBB</code>
<date>20190405</date>
<time>015553</time>
<timezone>GMT</timezone>
<mail>52170302634</mail>
</body>
</root>
<root>
<header>
<id>GGG-00009</id>
</header>
<body>
<user_name>HULK</user_name>
<code>UUU</code>
<date>20190405</date>
<time>015553</time>
<timezone>GMT</timezone>
</body>
</root>
</n0:AA_JSON_Out>


This is what need to produce:
[{"header":{},"body":{}},{"header":{},"body":{}}]

Below is the output code `enter code here`result.
[
{
"header":{
"id":"GGG-00009"
},
"body":{
"user_name":"RAMBO",
"code":"BBB",
"date":"20190405",
"time":"015553",
"timezone":"GMT",
"identfier":"52170302317"
}
},
{
"header":{
"id":"GGG-00009"
},
"body":{
"user_name":"HULK",
"code":"UUU",
"date":"20190405",
"time":"015553",
"timezone":"GMT"
}
}
]

XSLT Code:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">[<xsl:apply-templates select="@* | node()"/>]</xsl:template>

<!-- Object or Element Property-->
<xsl:template match="*">
<xsl:variable name="childName" select="name()" /><xsl:if test="not(contains($childName, 'root')) and not(contains($childName, 'n0'))">"<xsl:value-of select="normalize-space(name())"/>":</xsl:if><xsl:call-template name="Properties"/>
</xsl:template>

<!-- Array Element -->
<xsl:template match="*" mode="ArrayElement">
<xsl:call-template name="Properties"/>
</xsl:template>


<!-- Object Properties -->
<xsl:template name="Properties">
<xsl:variable name="childName" select="name()"/>
<xsl:choose><xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when><xsl:when test="count(*[name()=$childName]) > 1">

"<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] </xsl:when>
<xsl:otherwise><xsl:if test="not(contains($childName, 'root'))">{</xsl:if>
<xsl:apply-templates select="@*"/><xsl:apply-templates select="*"/><xsl:if test="not(contains($childName, 'root'))">}</xsl:if></xsl:otherwise>


</xsl:choose>

<xsl:if test="(contains($childName, 'body'))">}</xsl:if>
<xsl:if test="following-sibling::*">,</xsl:if>


</xsl:template>

<!-- Attribute Property -->
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
</xsl:template>
</xsl:stylesheet>

Please help me to provide XSLT code.
Thank you,
S,Saravannan

最佳答案

如果您可以迁移到 XSLT 3,那么您可以将 XML 映射到可直接序列化为 JSON 的 XPath 3.1 数组和映射:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
version="3.0">

<xsl:output method="json" indent="yes" />

<xsl:template match="/*">
<xsl:sequence
select="array {
root !
map:merge(
* ! map { local-name() : map:merge(* ! map:entry(local-name(), string())) }
)
}"/>
</xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/94AbWBr

XSLT 3 已针对 Java 实现, .NETC/C++/Python/PHP由 Saxon 9 以及 Altova XMLSpy 和 Raptor 提供。

例如,使用新的 Python API in Saxon-C 1.2.1你可以使用

import saxonc
import os

with saxonc.PySaxonProcessor() as proc:
print(proc.version)

xslt30_processor = proc.new_xslt30_processor()

xslt30_processor.set_cwd(os.getcwd())

xslt30_processor.apply_templates_returning_file(source_file = 'input-sample-to-transform-to-json1.xml', stylesheet_file = 'xml-to-xpath-31-array-and-maps-serialized-as-json1.xsl', output_file = 'example-json-output1.json')

针对输入示例运行给定的样式表并创建 JSON 结果文件。

关于json - 通过 XSLT 将 XML 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59352615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com