在"foreach“期间,我在属性值中增加一个数字。由于名称为“Produc-1”、“Produc-2”等的每个键的数组都有4个值,所以我只需要将数字从0增加到3,然后从0重复到3,直到处理对象结束为止。
我的原始JSON数据包含更多的产品。产品的数量是未知的,有时会有所不同。我将JSON数据最小化,以更好地适应这个问题。
除了属性从0增加到6之外,所有代码都工作得很好。
问题:
如何调整代码,使属性文本值从0增加到3,然后作为"foreach“的一部分重复。
JSON数据:
<data>
{
"store": {
"product-1": [0, 3, 2, 1],
"product-2": [4, 7, 6, 5]
},
"other": {
"Xxx": 42
}
}
</data>XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:item="http://www.example.org/1"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="fn"
expand-text="yes"
>
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-skip"/>
<!-- Parse JSON to XML -->
<xsl:template match="data">
<inventory>
<xsl:apply-templates select="json-to-xml(.)/*"/>
</inventory>
</xsl:template>
<!-- Attribute setting -->
<xsl:attribute-set name="datasheet-result">
<xsl:attribute name="unitRef">USD</xsl:attribute>
</xsl:attribute-set>
<!-- Template -->
<xsl:template match="*[@key = 'store']">
<xsl:for-each select="*/*">
<xsl:element name="item:{parent::*/@key}" use-attribute-sets="datasheet-result">
<xsl:attribute name="contextRef">period{position() - 1}</xsl:attribute>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:transform>结果:
<?xml version="1.0" encoding="UTF-8"?>
<inventory xmlns:item="http://www.example.org/1">
<item:product-1 unitRef="USD" contextRef="period0">0</item:product-1>
<item:product-1 unitRef="USD" contextRef="period1">3</item:product-1>
<item:product-1 unitRef="USD" contextRef="period2">2</item:product-1>
<item:product-1 unitRef="USD" contextRef="period3">1</item:product-1>
<item:product-2 unitRef="USD" contextRef="period4">4</item:product-2>
<item:product-2 unitRef="USD" contextRef="period5">7</item:product-2>
<item:product-2 unitRef="USD" contextRef="period6">6</item:product-2>
<item:product-2 unitRef="USD" contextRef="period7">5</item:product-2>
</inventory>通缉结果:
<?xml version="1.0" encoding="UTF-8"?>
<inventory xmlns:item="http://www.example.org/1">
<item:product-1 unitRef="USD" contextRef="period0">0</item:product-1>
<item:product-1 unitRef="USD" contextRef="period1">3</item:product-1>
<item:product-1 unitRef="USD" contextRef="period2">2</item:product-1>
<item:product-1 unitRef="USD" contextRef="period3">1</item:product-1>
<item:product-2 unitRef="USD" contextRef="period0">4</item:product-2>
<item:product-2 unitRef="USD" contextRef="period1">7</item:product-2>
<item:product-2 unitRef="USD" contextRef="period2">6</item:product-2>
<item:product-2 unitRef="USD" contextRef="period3">5</item:product-2>
</inventory>发布于 2021-05-24 17:56:18
Siebe给出了一个使用两个嵌套for-each的示例,以便position()与您想要的结果匹配,另一个选项是使用xsl:number,例如<xsl:attribute name="contextRef">period<xsl:number start-at="0"/></xsl:attribute>是最短的示例。
至于xsl:number是如何工作的,在其最短的<xsl:number/>中,基本上将samle "type“的兄弟节点计数为上下文节点,并输出该数字加1。一般而言,使用它有多种选项,请参阅XSLT的任何介绍,例如,https://cranesoftwrights.github.io/books/ptux/index.htm免费下载XSLT 2和1介绍的PDF副本(“使用XSLT和XPath的实际转换”),并在第8章“构造结果树”中有第3节“编号指令”/“源树编号”。
当然,您可以自由浏览规范部分https://www.w3.org/TR/xslt-30/#number。
发布于 2021-05-24 16:28:21
使用xsl:for-每次2次如下所示:
<xsl:template match="*[@key = 'store']">
<xsl:for-each select="*">
<xsl:for-each select="*">
<xsl:element name="item:{parent::*/@key}" use-attribute-sets="datasheet-result">
<xsl:attribute name="contextRef">period{position() - 1}</xsl:attribute>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:template>https://stackoverflow.com/questions/67675095
复制相似问题