数据类型映射:查询键名称和 return 值
Datatype map: Query key names and return values
我正在尝试使用密钥名称查询 XML 映射(使用 json-to-xml 解析 JSON 文件的结果),以获取密钥“for-each”期间的值。
我可以使用键索引进行查询,请参阅 test-1。 Test-2 和 Test-3 失败了,但我认为我在处理对 XML 映射的查询方面的语法错误。
我让 test-1 处于激活状态,并注释掉 test-2/test-3,因为该设置显示了想要的结果。未使用余额,但保留余额只是为了确保它不会传递给结果。
JSON:
<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>
XSL:
<?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>
在 for 循环内部,如果你要 return Map XML 结构:<xsl:sequence select="."/>
你会看到它看起来像这样:
<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>
因此,对于您的第二个测试,为了提取开始日期,select @key
属性的元素:
<!-- Test [2] -->
<!-- Extract startDate value by name -->
<xsl:value-of select="*[@key='startDate']"/>
我正在尝试使用密钥名称查询 XML 映射(使用 json-to-xml 解析 JSON 文件的结果),以获取密钥“for-each”期间的值。
我可以使用键索引进行查询,请参阅 test-1。 Test-2 和 Test-3 失败了,但我认为我在处理对 XML 映射的查询方面的语法错误。
我让 test-1 处于激活状态,并注释掉 test-2/test-3,因为该设置显示了想要的结果。未使用余额,但保留余额只是为了确保它不会传递给结果。
JSON:
<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>
XSL:
<?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>
在 for 循环内部,如果你要 return Map XML 结构:<xsl:sequence select="."/>
你会看到它看起来像这样:
<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>
因此,对于您的第二个测试,为了提取开始日期,select @key
属性的元素:
<!-- Test [2] -->
<!-- Extract startDate value by name -->
<xsl:value-of select="*[@key='startDate']"/>