gpt4 book ai didi

xml - 使用 XSLT 将 XML 转换为 JSON 并删除命名空间

转载 作者:行者123 更新时间:2023-12-04 16:54:54 25 4
gpt4 key购买 nike

我是 XSLT 的新手。

我必须使用 XSLT 将 XML 转换为 JSON。

我有以下 xml:

<getEndUserCriteriaListForRangeResponse xmlns="http://xxxx.xxxx.xx.com/">
<endUserCriteriaList>
<ns0:endUserCriteria xmlns:ns0="http://xxxx.xxxx.xx.com">
<ns0:defaultValue>
<ns0:customValue>PARAMETER</ns0:customValue>
<ns0:eucValue>PARAMETER</ns0:eucValue>
<ns0:eucValueId>PARAMETER</ns0:eucValueId>
</ns0:defaultValue>
<ns0:eucId>PARAMETER</ns0:eucId>
<ns0:label>PARAMETER</ns0:label>
<ns0:ranges>
<ns0:id>PARAMETER</ns0:id>
<ns0:rangeName>PARAMETER</ns0:rangeName>
</ns0:ranges>
<ns0:status>PARAMETER</ns0:status>
<ns0:unit>PARAMETER</ns0:unit>
<ns0:values>
<ns0:customValue>PARAMETER</ns0:customValue>
<ns0:eucValue>PARAMETER</ns0:eucValue>
<ns0:eucValueId>PARAMETER</ns0:eucValueId>
</ns0:values>
<ns0:weight>PARAMETER</ns0:weight>
</ns0:endUserCriteria>
</endUserCriteriaList>
</getEndUserCriteriaListForRangeResponse>

这是 XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />
<xsl:template match="/">
<xsl:copy-of select="/*" />
</xsl:template>
</xsl:stylesheet>

生成的 JSON 是:
{"@xmlns":"http://xxxx.xxxx.xx.com/","endUserCriteriaList":{"ns0:endUserCriteria":         {"@xmlns:ns0":"http://xxxx.xxxx.xx.com","ns0:defaultValue":{"ns0:customValue":"PARAMETER","ns0:eucValue":"PARAMETER","ns0:eucValueId":"PARAMETER"},"ns0:eucId":"PARAMETER","ns0:label":"PARAMETER","ns0:ranges":{"ns0:id":"PARAMETER","ns0:rangeName":"PARAMETER"},"ns0:status":"PARAMETER","ns0:unit":"PARAMETER","ns0:values":{"ns0:customValue":"PARAMETER","ns0:eucValue":"PARAMETER","ns0:eucValueId":"PARAMETER"},"ns0:weight":"PARAMETER"}}}

在生成的 JSON 中,我需要删除“ns0:”。
我怎么做?

最佳答案

这是一个通用的 XSLT,它可以从源文档中去除所有 namespace :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="@* | node()[not(self::*)]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

</xsl:stylesheet>

在您的输入上运行时,结果是:
<getEndUserCriteriaListForRangeResponse>
<endUserCriteriaList>
<endUserCriteria>
<defaultValue>
<customValue>PARAMETER</customValue>
<eucValue>PARAMETER</eucValue>
<eucValueId>PARAMETER</eucValueId>
</defaultValue>
<eucId>PARAMETER</eucId>
<label>PARAMETER</label>
<ranges>
<id>PARAMETER</id>
<rangeName>PARAMETER</rangeName>
</ranges>
<status>PARAMETER</status>
<unit>PARAMETER</unit>
<values>
<customValue>PARAMETER</customValue>
<eucValue>PARAMETER</eucValue>
<eucValueId>PARAMETER</eucValueId>
</values>
<weight>PARAMETER</weight>
</endUserCriteria>
</endUserCriteriaList>
</getEndUserCriteriaListForRangeResponse>

我认为您的流程管道中有一些步骤会将其转换为您正在寻找的 JSON。

关于xml - 使用 XSLT 将 XML 转换为 JSON 并删除命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23260382/

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