如何获取每个循环内节点的当前位置和全局位置?

问题描述 投票:0回答:1

我已经输入了有关结构的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>的开头生成索引,而不是基于整个输入文件的索引。如何解决呢?还是我需要从头开始重写脚本并以不同的方式处理它?

xml xslt xslt-1.0
1个回答
0
投票

我将简化在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="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <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

© www.soinside.com 2019 - 2024. All rights reserved.