在展平数据之前将属性添加到匹配的解析 json-to-xml 映射

问题描述

我正在寻找一种方法来匹配每个最高级别的数据,并为该级别添加特定属性。 由于属性将不同且特定于最高级别的数据,因此我假设需要使用模板。应用属性后,可以将数据展平。

注意!生成的 XML 中不需要来自 JSON 数据的高级密钥,它们只需要了解每个高级密钥组应该具有特定的属性集。

澄清一下,“结果”和“想要的结果”之间的当前区别在于,“结果”在所有给定的高级键上都添加属性“period0”,而“想要的结果”是每个高级键(例如“一般”等)在被展平之前有自己的定义它的属性的方式。在“想要的结果”中添加了一些注释以进一步说明来源。

https://xsltfiddle.liberty-development.net/nbiE1a1/2

XML 数据源文件

<data>
{
    "general": {
      "Language": "English","Country": "Sweden"
    },"units-deFinitions": {
      "SEK": "iso4217:SEK"
    }
  }
</data>

XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:root="http://www.example.org/1"
  xmlns:flat="http://www.example.org/2"
  exclude-result-prefixes="xs"
  expand-text="yes">

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

  <xsl:template match="data">
    <root:report>
      <xsl:apply-templates select="json-to-xml(.)//*[@key and not(*)]"/>
    </root:report>
  </xsl:template>
  
  <!-- Generic for all flattened data-->
  
  <xsl:template match="*[@key]">
    <xsl:element name="flat:{@key}">
      <xsl:attribute name="contextRef">period0</xsl:attribute>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
  
  <!-- Template specific for "general" -->
  
  
  <!-- Template specific for "units-deFinitions" -->

</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<root:report xmlns:root="http://www.example.org/1" xmlns:flat="http://www.example.org/2">
   <flat:Language contextRef="period0">English</flat:Language>
   <flat:Country contextRef="period0">Sweden</flat:Country>
   <flat:SEK contextRef="period0">iso4217:SEK</flat:SEK>
</root:report>

想要的结果:

<?xml version="1.0" encoding="UTF-8"?>
<root:report xmlns:root="http://www.example.org/1" xmlns:flat="http://www.example.org/2">
   <!--Origins from "general"-->
   <flat:Language contextRef="period0">English</flat:Language>
   <flat:Country contextRef="period0">Sweden</flat:Country>
   <!--Origins from "units-deFinitions"-->
   <flat:SEK contextRef="balance0">iso4217:SEK</flat:SEK>
</root:report>

解决方法

所以给你一个使用模板匹配参数的例子:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:root="http://www.example.org/1"
  xmlns:flat="http://www.example.org/2"
  exclude-result-prefixes="xs"
  expand-text="yes">
    
  <xsl:mode on-no-match="shallow-skip"/>

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

  <xsl:template match="data">
    <root:report>
      <xsl:apply-templates select="json-to-xml(.)/*"/>
    </root:report>
  </xsl:template>
  
  <!-- Generic for all flattened data-->
  
  <xsl:template match="*[@key and not(*)]">
    <xsl:param name="contextRef" tunnel="yes"/>
    <xsl:element name="flat:{@key}">
      <xsl:attribute name="contextRef" select="$contextRef"/>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
  
  <!-- Template specific for "general" -->
  <xsl:template match="*[@key = 'general']">
    <xsl:apply-templates>
        <xsl:with-param name="contextRef" tunnel="yes" select="'period0'"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <!-- Template specific for "units-definitions" -->

  <xsl:template match="*[@key = 'units-definitions']">
    <xsl:apply-templates>
        <xsl:with-param name="contextRef" tunnel="yes" select="'balance0'"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/nbiE1a1/3