微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解析 JSON 文件数据后,在结果 XML 中添加注释

如何解决解析 JSON 文件数据后,在结果 XML 中添加注释

由于扁平化解析的 JSON 数据,我需要添加一些注释作为标题代码行分隔符,以便更好地了解 XML 结果。评论字段中的文本可以来自 JSON 的高级键,也可以在创建评论时手动添加

我尝试添加在 XSL 中创建注释的标准方法,但由于我使用的模板匹配多个节点,结果是一个迭代,其中注释出现在每个转换元素的顶部。

如果推荐,也可以通过单独的模板添加评论

您可以在此处找到代码https://xsltfiddle.liberty-development.net/gVAkJ3X/4

以下是代码的摘录:

JSON 数据:

<data>

{
  "ix_hidden": [
    {
    "CompanyName": "Link Inc","OrganisationNumber": "123"
    }
  ],"other": [
    {
      "SomethingElse": "Juice"
    }
  ]

}

</data>

XSL:

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xbrli="http://www.example.com/1"
  xmlns:rot="http://www.example.com/2"
  >

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

<!-- Parse JSON to XML -->

  <xsl:template match="data">
  <report>
    <xsl:apply-templates select="json-to-xml(.)/*"/>
  </report>
</xsl:template>


<!-- Flatten data,exlude high-level key names-->

<xsl:template match="*[@key and not(*)]">
  <xsl:element name="{@key}">
    <xsl:value-of select="."/>
  </xsl:element>

<!-- Add comments equal as the key values from parsed JSON-->

<!-- Add comment for "ix_hidden" -->
<xsl:comment>Group:ix_hidden</xsl:comment>

<!-- Add comment for "other" -->
<xsl:comment>Group:other</xsl:comment>

</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns:xbrli="http://www.example.com/1" xmlns:rot="http://www.example.com/2">
   <CompanyName>Link Inc</CompanyName>
   <!--Group:ix_hidden-->
   <!--Group:other-->
   <OrganisationNumber>123</OrganisationNumber>
   <!--Group:ix_hidden-->
   <!--Group:other-->
   <SomethingElse>Juice</SomethingElse>
   <!--Group:ix_hidden-->
   <!--Group:other-->
</report>

想要的结果

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns:xbrli="http://www.example.com/1" xmlns:rot="http://www.example.com/2">
   <!--Group:ix_hidden-->
   <CompanyName>Link Inc</CompanyName>
   <OrganisationNumber>123</OrganisationNumber>
   <!--Group:other-->
   <SomethingElse>Juice</SomethingElse>
</report>

解决方法

匹配数组并输出注释,然后应用模板:

BlocProvider
,

似乎用单独的模板添加评论工作正常。 https://xsltfiddle.liberty-development.net/gVAkJ3X/5

使用此 XSL 将获得适当的注释。请注意,注释值是硬编码的,不是从 JSON 解析中获取的。首选的解决方案是重用 JSON 键值作为注释。

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  >

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

<!-- Parse JSON to XML -->

  <xsl:template match="data">
  <report>
    <xsl:apply-templates select="json-to-xml(.)/*"/>
  </report>
</xsl:template>


<!-- Flatten data,exlude high-level key names-->

<xsl:template match="*[@key and not(*)]">
  <xsl:element name="{@key}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

<!-- Add comments equal as the key values from parsed JSON-->

<xsl:template match="fn:array[@key = 'ix_hidden']">
  <xsl:text>&#xa;&#xa;</xsl:text>
  <xsl:comment>ix_hidden</xsl:comment>
  <xsl:text>&#xa;&#xa;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="fn:array[@key = 'other']">
  <xsl:text>&#xa;&#xa;</xsl:text>
  <xsl:comment>certifications</xsl:comment>
  <xsl:text>&#xa;&#xa;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。