繁体   English   中英

如何在 for-each 循环中获取节点的当前和全局位置?

[英]How to get the current and global position of a node inside a for-each loop?

我有关于结构的输入xml文件:

<TabularXml>
  <Sheet>
    <Row id="2">
      <Cell id="1" name="A" type="String">aaa</Cell>
      <Cell id="2" name="B" type="String">1</Cell>
    </Row>
    <Row id="3">
      <Cell id="1" name="A" type="String">aaa</Cell>
      <Cell id="2" name="B" type="String">2</Cell>
    </Row>
    <Row id="3">
      <Cell id="1" name="A" type="String">bbb</Cell>
      <Cell id="2" name="B" type="String">3</Cell>
    </Row>
    <Row id="4">
      <Cell id="1" name="A" type="String">ccc</Cell>
      <Cell id="2" name="B" type="String">4</Cell>
    </Row>  
  </Sheet>
</TabularXml>

我想要输出:

<Beholdninger>
   <Unique>3</Unique>
   <AllePoster>
      <A>aaa</A>
      <p><B>1<B/></p>
      <p><B>2<B/></p>
      <A>bbb</A>
      <p><B>3<B/></p>
      <A>ccc</A>
      <p><B>4<B/></p>
   </AllePoster>
</Beholdninger>

我当前的 xslt 代码:

  </msxsl:script>
    <xsl:key name="product" match="//Row/Cell[@name = 'A']/text()" use="." />
    <xsl:template match="Sheet">
        <Beholdninger>                          

            <xsl:for-each select="//Row/Cell[@name = 'A']/text()[generate-id() = generate-id(key('product',.)[1])]">                
                <xsl:if test="position()=last()">
                    <Unique><xsl:value-of select="last()"/></Unique>
                </xsl:if>
            </xsl:for-each>

            <AllePoster>

            <xsl:for-each select="//Row/Cell[@name = 'A']/text()[generate-id() = generate-id(key('product',.)[1])]">

                <A><xsl:value-of select="."/></A>                                   
                <xsl:for-each select="key('product',.)">

                    <xsl:variable select="position()" name='x'/>    
                    <p><B><xsl:value-of select="//Row[@id = $x]/Cell[@name = 'B']"/></B></p>                                                        

                </xsl:for-each>                 
            </xsl:for-each>

            </AllePoster>

        </Beholdninger>
</xsl:template>
</xsl:stylesheet>

我在哪里得到:

<Beholdninger>
   <Unique>3</Unique>
   <AllePoster>
      <A>aaa</A>
      <p><B>1<B/></p>
      <p><B>2<B/></p>
      <A>bbb</A>
      <p><B>1<B/></p>
      <A>ccc</A>
      <p><B>1<B/></p>
   </AllePoster>
</Beholdninger>

这是因为position()函数从每个唯一的<A>的开头生成索引,而不是基于整个输入文件的索引。 如何解决这个问题? 或者也许我需要从头开始重写脚本并以不同的方式处理它?

我会简化匹配Row的键,然后编写模板来转换单元格:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="a-group" match="Row" use="Cell[@name = 'A']"/>

  <xsl:template match="Sheet">
     <Beholdninger>
         <xsl:variable name="group-heads" select="Row[generate-id() = generate-id(key('a-group', Cell[@name = 'A'])[1])]"/>
         <Unique>
             <xsl:value-of select="count($group-heads)"/>
         </Unique>
         <AllePoster>
             <xsl:apply-templates select="$group-heads"/>
         </AllePoster>
     </Beholdninger> 
  </xsl:template>

  <xsl:template match="Row">
      <xsl:apply-templates select="Cell[@name = 'A'] | key('a-group', Cell[@name = 'A'])/Cell[@name = 'B']"/>
  </xsl:template>

  <xsl:template match="Cell[@name = 'A']">
      <A>
          <xsl:value-of select="."/>
      </A>
  </xsl:template>

  <xsl:template match="Cell[@name = 'B']">
      <p>
          <B>
              <xsl:value-of select="."/>
          </B>
       </p>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/ejivJsd/1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM