首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >外汇中“位置()”的断点增加

外汇中“位置()”的断点增加
EN

Stack Overflow用户
提问于 2021-05-24 15:47:35
回答 2查看 61关注 0票数 0

在"foreach“期间,我在属性值中增加一个数字。由于名称为“Produc-1”、“Produc-2”等的每个键的数组都有4个值,所以我只需要将数字从0增加到3,然后从0重复到3,直到处理对象结束为止。

我的原始JSON数据包含更多的产品。产品的数量是未知的,有时会有所不同。我将JSON数据最小化,以更好地适应这个问题。

除了属性从0增加到6之外,所有代码都工作得很好。

问题:

如何调整代码,使属性文本值从0增加到3,然后作为"foreach“的一部分重复。

JSON数据:

代码语言:javascript
复制
<data>
{
  "store": {
    "product-1": [0, 3, 2, 1],
    "product-2": [4, 7, 6, 5]
  },
  "other": {
    "Xxx": 42
  }
}
</data>

XSL:

代码语言:javascript
复制
<?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>

结果:

代码语言:javascript
复制
<?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>

通缉结果:

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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

票数 2
EN

Stack Overflow用户

发布于 2021-05-24 16:28:21

使用xsl:for-每次2次如下所示:

代码语言:javascript
复制
  <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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67675095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档