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

标签 xml json xslt namespaces

我是 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/

相关文章:

c# - DateTime.Now 没有小数秒

javascript - 在 express.js 中循环遍历 json 数据

java - 如何在 Java 中从 XML 创建 PDF?

xml - 连接 XMLType 的变量的好方法是什么

c# - 通过 linq 从 xml 检索数据时出现问题

json - MarkLogic 7 REST API - 错误请求错误

json - 有一种干净的DRY方法可以从HTTP请求JSON更新多个textViews吗?

java - 如何在 java 中运行 saxon xslt 转换

javascript - 如何在 xslt 中包含 javaScript 文件

java - XMLUnit : How to register a self-closing element as different to a non-self-closing element?