如何使用模板 XSLT 获取位置或索引



XML:

  <Grandparent>
         <Parent>
                 <Children>
                       <Info>
                           <Name>
                              <label id="chname"/>
                           </Name>
                       </Info>
                 </Children>
                 <Children>
                       <Info>
                           <Name>
                              <label id="chname"/>
                           </Name>
                       </Info>
                 </Children
                <Children>
                       <Info>
                           <Name>
                              <label id="chname"/>
                           </Name>
                       </Info>
                 </Children
         </Parent>
    </Grandparent>

XSLT:

<xsl:template match"/">
   <xsl:apply-templates select="GrandParent/Parent/Children" />
</xsl:template>

<xsl:template match="Children">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>
  <xsl:template match="Children/Info/Name/label">
    <xsl:copy>
      <xsl:variable name="childCtr" select="Parent/Children[position()]"/>
        <xsl:attribute name="text">
                     <xsl:value-of select="$childCtr"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()" />
     </xsl:copy>
  </xsl:template>

  <!--Identity template copies content forward -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

如何在向标签标签添加属性并获取整个"子"节点的同时,使用模板获取"子项"的索引或位置?

喜欢:

 Parent[0] = Children1,
 Parent[1] = Children2,
 Parent[2] = Children3

我怎么能得到这种东西?我需要一些帮助。提前致谢

在没有列出

预期输出的情况下,很难说你想要什么,但也许就是这样......

此 XSLT 1.0 样式表...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="label">
  <label text="{count(../../../preceding-sibling::Children) + 1}">
    <xsl:apply-templates select="@*|node()"/>
  </label>
</xsl:template>
</xsl:stylesheet>

。当应用于此输入时...

<Grandparent>
    <Parent>
        <Children>
            <Info>
                <Name>
                    <label id="chname"/>
                </Name>
            </Info>
        </Children>
        <Children>
            <Info>
                <Name>
                    <label id="chname"/>
                </Name>
            </Info>
        </Children>
        <Children>
            <Info>
                <Name>
                    <label id="chname"/>
                </Name>
            </Info>
        </Children>
    </Parent>
</Grandparent>

。会屈服...

<Grandparent>
  <Parent>
    <Children>
      <Info>
        <Name>
          <label text="1" id="chname" />
        </Name>
      </Info>
    </Children>
    <Children>
      <Info>
        <Name>
          <label text="2" id="chname" />
        </Name>
      </Info>
    </Children>
    <Children>
      <Info>
        <Name>
          <label text="3" id="chname" />
        </Name>
      </Info>
    </Children>
  </Parent>
</Grandparent>

隧道(XSLT 2.0 功能)由子模板设置的参数:

<xsl:template match="Children">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()">
            <xsl:with-param name="childPosition" select="position()" tunnel="yes"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>
<xsl:template match="Children/Info/Name/label">
    <xsl:param name="childPosition" tunnel="yes"/>
    <xsl:copy>
        <xsl:attribute name="text">
            <xsl:value-of select="$childPosition"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

试试这个:

  <xsl:variable name="childCtr" select="count(ancestor::Children/preceding-sibling::Children)"/>

最新更新