Json 数据类型映射:查询键名称和返回值
我试图使用key name查询一个XML映射(用JSON将JSON文件解析为XML的结果),以在“for each”期间获取键值 我能够使用键索引进行查询,请参阅test-1。Test-2和Test-3失败了,但我认为在如何处理对XML映射的查询方面,我的语法是错误的 我让test-1保持激活状态,并注释掉test-2/test-3,因为该设置显示了想要的结果。余额并没有被使用,只是为了确保它不会传递给结果 JSON:Json 数据类型映射:查询键名称和返回值,json,xml,xslt,xslt-3.0,Json,Xml,Xslt,Xslt 3.0,我试图使用key name查询一个XML映射(用JSON将JSON文件解析为XML的结果),以在“for each”期间获取键值 我能够使用键索引进行查询,请参阅test-1。Test-2和Test-3失败了,但我认为在如何处理对XML映射的查询方面,我的语法是错误的 我让test-1保持激活状态,并注释掉test-2/test-3,因为该设置显示了想要的结果。余额并没有被使用,只是为了确保它不会传递给结果 JSON: <data> { "period": {
<data>
{
"period": {
"0": {"startDate": "2016-01-01","endDate": "2016-12-31"},
"1": {"startDate": "2015-01-01","endDate": "2015-12-31"}
},
"balance": {
"0": {"instant": "2016-01-01"},
"1": {"instant": "2015-01-01"}
}
}
</data>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:root="http://www.example.com/1"
xmlns:periods="http://www.example.com/2"
expand-text="yes"
>
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-skip"/>
<!-- Parse JSON to XML -->
<xsl:template match="data">
<root:report>
<xsl:apply-templates select="json-to-xml(.)/*"/>
</root:report>
</xsl:template>
<!-- Process "period" -->
<xsl:template match="*[@key = 'period']">
<xsl:for-each select="./*">
<periods:startDate>
<!-- Test [1] -->
<!-- Extract startDate value by index -->
<xsl:value-of select="./*[1]"/>
<!-- Test [2] -->
<!-- Extract startDate value by name -->
<!-- <xsl:value-of select="startDate"/> -->
<!-- Test [3] -->
<!-- Extract startDate by function map:get -->
<!-- <xsl:variable name="$startDate" select="What to put here?"/>
<xsl:value-of select="map:get($startDate)"/> -->
</periods:startDate>
</xsl:for-each>
</xsl:template>
</xsl:transform>
{
“期间”:{
“0”:{“开始日期”:“2016-01-01”,“结束日期”:“2016-12-31”},
“1”:{“开始日期”:“2015-01-01”,“结束日期”:“2015-12-31”}
},
“余额”:{
“0”:{“即时”:“2016-01-01”},
“1”:{“即时”:“2015-01-01”}
}
}
XSL:
<data>
{
"period": {
"0": {"startDate": "2016-01-01","endDate": "2016-12-31"},
"1": {"startDate": "2015-01-01","endDate": "2015-12-31"}
},
"balance": {
"0": {"instant": "2016-01-01"},
"1": {"instant": "2015-01-01"}
}
}
</data>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:root="http://www.example.com/1"
xmlns:periods="http://www.example.com/2"
expand-text="yes"
>
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-skip"/>
<!-- Parse JSON to XML -->
<xsl:template match="data">
<root:report>
<xsl:apply-templates select="json-to-xml(.)/*"/>
</root:report>
</xsl:template>
<!-- Process "period" -->
<xsl:template match="*[@key = 'period']">
<xsl:for-each select="./*">
<periods:startDate>
<!-- Test [1] -->
<!-- Extract startDate value by index -->
<xsl:value-of select="./*[1]"/>
<!-- Test [2] -->
<!-- Extract startDate value by name -->
<!-- <xsl:value-of select="startDate"/> -->
<!-- Test [3] -->
<!-- Extract startDate by function map:get -->
<!-- <xsl:variable name="$startDate" select="What to put here?"/>
<xsl:value-of select="map:get($startDate)"/> -->
</periods:startDate>
</xsl:for-each>
</xsl:template>
</xsl:transform>
想要的结果
<?xml version="1.0" encoding="UTF-8"?>
<root:report xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:periods="http://www.example.com/2"
xmlns:root="http://www.example.com/1">
<periods:startDate>2016-01-01</periods:startDate>
<periods:startDate>2015-01-01</periods:startDate>
</root:report>
2016-01-01
2015-01-01
在for循环内部,如果要返回Map XML结构:
您将看到它如下所示:
<map xmlns="http://www.w3.org/2005/xpath-functions" key="0">
<string key="startDate">2016-01-01</string>
<string key="endDate">2016-12-31</string>
</map>