简体   繁体   English

使用 XSLT 1.0 在单元素 JSON 中包装 XML 文档

[英]Wrapping XML document in one-element JSON using XSLT 1.0

I'm trying to transform an XML document to be in single-line and wrap it in a one-element JSON.我正在尝试将 XML 文档转换为单行并将其包装在单元素 JSON 中。 Using XSLT 1.0使用 XSLT 1.0

The problem is, XSL generates double quotes in the xmlns definitions so the resulting JSON is invalid.问题是,XSL 在 xmlns 定义中生成双引号,因此生成的 JSON 无效。

This is my input:这是我的输入:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<otm:Transmission xmlns:otm='http://xmlns.oracle.com/apps/otm/transmission/v6.4' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <otm:TransmissionHeader/>
  <otm:TransmissionBody>
    <otm:GLogXMLElement>
      <otm:Invoice>
        <otm:Payment>
          <otm:PaymentHeader>
            <otm:DomainName>CompanyX</otm:DomainName>
            <otm:TransactionCode>EX</otm:TransactionCode>
            <otm:InvoiceDate>
              <otm:GLogDate>20220414000000</otm:GLogDate>
            </otm:InvoiceDate>
          </otm:PaymentHeader>
        </otm:Payment>
      </otm:Invoice>
    </otm:GLogXMLElement>
  </otm:TransmissionBody>
</otm:Transmission>

This is what I'm getting:这就是我得到的:

{"jsonElement":"<otm:Transmission xmlns:otm="http://xmlns.oracle.com/apps/otm/transmission/v6.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><otm:TransmissionHeader/><otm:TransmissionBody><otm:GLogXMLElement><otm:Invoice><otm:Payment><otm:PaymentHeader><otm:DomainName>CompanyX</otm:DomainName><otm:TransactionCode>EX</otm:TransactionCode><otm:InvoiceDate><otm:GLogDate>20220414000000</otm:GLogDate></otm:InvoiceDate></otm:PaymentHeader></otm:Payment></otm:Invoice></otm:GLogXMLElement></otm:TransmissionBody></otm:Transmission>"}

The XSL that I use:我使用的 XSL:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:otm='http://xmlns.oracle.com/apps/otm/transmission/v6.4'>
<xsl:output method="text" indent="no" suppress-indentation="otm:Transmission"/>
<xsl:strip-space elements="*" />

    <xsl:template match="/">
{"jsonElement":"<xsl:apply-templates select="*"/>"}
    </xsl:template>

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

</xsl:stylesheet>

As you can see, the JSON is invalid due to double quotes in the xmlns definitons.如您所见,由于 xmlns 定义中的双引号,JSON 无效。

I tried several approaches and I am not able to get rid of the double quotes.我尝试了几种方法,但我无法摆脱双引号。 In the input, they are single quotes but XSL is generating them differently.在输入中,它们是单引号,但 XSL 生成它们的方式不同。

What would be the best approach to have a valid JSON result?获得有效 JSON 结果的最佳方法是什么? The XML in the JSON has to be 1:1 copy of the input but transformed into a single line and I can only use XSLT 1.0 JSON 中的 XML 必须是输入的 1:1 副本,但转换为单行,我只能使用 XSLT 1.0

An XSLT 1.0 processor is going to reject the suppress-indentation attribute, and it's going to output the text of the source document without markup. XSLT 1.0 处理器将拒绝suppress-indentation属性,并且将输出不带标记的源文档的文本。 Like @MartinHonnen, I don't see how any XSLT processor can give you the output you claim to be getting.像@MartinHonnen 一样,我看不出任何 XSLT 处理器如何能够为您提供您声称获得的输出。

In XSLT 3.0 you can do在 XSLT 3.0 中你可以做

<xsl:output method="json">

<xsl:template match="/">
  <xsl:map key="'jsonElement'"
           select="serialize(., map{'method':'xml'})"/>
</xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM