解析JSON个文件数据后,对结果XML添加注释
After parsing JSON file data, add comments to result XML
由于扁平化解析的 JSON 数据,我需要添加一些注释作为标题和代码行分隔符,以便更好地了解 XML 结果。评论字段中的文本可以来自 JSON 中的 high-level 键,也可以在创建评论时手动添加。
我尝试添加在 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>
似乎使用单独的模板添加评论效果很好。
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>

</xsl:text>
<xsl:comment>ix_hidden</xsl:comment>
<xsl:text>

</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="fn:array[@key = 'other']">
<xsl:text>

</xsl:text>
<xsl:comment>certifications</xsl:comment>
<xsl:text>

</xsl:text>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
匹配数组并输出注释,然后应用模板:
<!-- Add comments equal as the key values from parsed JSON-->
<xsl:template match="*:map/*:array[@key]">
<xsl:comment expand-text="yes">Group:{@key}</xsl:comment>
<xsl:apply-templates/>
</xsl:template>
由于扁平化解析的 JSON 数据,我需要添加一些注释作为标题和代码行分隔符,以便更好地了解 XML 结果。评论字段中的文本可以来自 JSON 中的 high-level 键,也可以在创建评论时手动添加。
我尝试添加在 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>
似乎使用单独的模板添加评论效果很好。 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>

</xsl:text>
<xsl:comment>ix_hidden</xsl:comment>
<xsl:text>

</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="fn:array[@key = 'other']">
<xsl:text>

</xsl:text>
<xsl:comment>certifications</xsl:comment>
<xsl:text>

</xsl:text>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
匹配数组并输出注释,然后应用模板:
<!-- Add comments equal as the key values from parsed JSON-->
<xsl:template match="*:map/*:array[@key]">
<xsl:comment expand-text="yes">Group:{@key}</xsl:comment>
<xsl:apply-templates/>
</xsl:template>