如何解决在展平数据之前将属性添加到匹配的解析 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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。