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

Add attributes to matched parsed json-to-xml map prior to flatten data

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

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

澄清一下,“result”和“wanted result”目前的区别在于,“result”在所有给定的high-level keys上添加了属性“period0”,同时“wanted result”是每个high- level 键(例如“general”等)在被展平之前有自己的方式来定义它的属性。在“想要的结果”中添加了一些评论,以进一步澄清来源。

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