Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在展平数据之前,向匹配的解析json到xml映射添加属性_Xml_Xslt_Xslt 3.0 - Fatal编程技术网

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

在展平数据之前,向匹配的解析json到xml映射添加属性,xml,xslt,xslt-3.0,Xml,Xslt,Xslt 3.0,我正在寻找一种方法来匹配每个最高级别的数据,并为该级别添加特定属性。 由于属性将不同,并且特定于最高级别的数据,因此我假设需要使用模板。应用属性后,可以展平数据 注意!在生成的XML中不需要JSON数据中的高级键,它们只需要理解每个高级键组都应该有一组特定的属性 要澄清的是,“结果”和“想要的结果”之间当前的区别是,“结果”在所有给定的高级键上添加属性“period0”,同时“想要的结果”是每个高级键(例如“general”等)在被展平之前都有自己定义属性的方式。在“通缉结果”中添加了一些注释,

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

注意!在生成的XML中不需要JSON数据中的高级键,它们只需要理解每个高级键组都应该有一组特定的属性

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

XML数据源文件

<data>
{
    "general": {
      "Language": "English",
      "Country": "Sweden"
    },

    "units-definitions": {
      "SEK": "iso4217:SEK"
    }
  }
</data>

{
“一般”:{
“语言”:“英语”,
“国家”:“瑞典”
},
“单位定义”:{
“SEK”:“iso4217:SEK”
}
}
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>

第0期
结果:

<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>

英语
瑞典
iso4217:SEK
想要的结果:

<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>

英语
瑞典
iso4217:SEK

因此,给您一个使用模板与参数匹配的示例:

<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>


如果您认为需要一个模板,那么编写一个模板,并使用
xsl:with-param
(可能还有tunnel-params)在
xsl:apply-templates
上向下传递任何值。另一方面,我认为只要读取叶模板中的祖先::*/@key就可以根据JSON的XML表示中的祖先计算值。因此,我仍然无法确定您到底想要或需要哪些值。在这种特定情况下,“general”的所有子项都应该具有属性“contextRef=”period0“,而“units Definitions”的所有子项都应该具有属性“contextRef=”period0”。然而,结果应该是平淡的。