首页 文章

XSLT 2:将具有递增值的元素添加到列表中

提问于
浏览
1

我是XSLT的新手,花了好几个小时试图找出一个似乎有点微不足道的问题的解决方案 .

我有一个xml文档,其中包含如下列表:

<Header>
    <URLList>
      <URLItem type="type1">
        <URL></URL>
      </URLItem>
      <URLItem type="type2">
        <URL>2</URL>
      </URLItem>
    </URLList>
  </Header>

如果它不存在,我现在需要为每个URLItem添加一个“ID”元素 . ID元素的值必须是递增的值 .

xml最终应该如下所示:

<Header>
    <URLList>
      <URLItem type="type1">
        <ID>1</ID>
        <URL></URL>
      </URLItem>
      <URLItem type="type2">
        <ID>2</ID>
        <URL>2</URL>
      </URLItem>
    </URLList>
  </Header>

我一直在尝试各种各样的东西,但无法让它正常工作 .

例如,如果我尝试使用模板来匹配List,则无法获得正确的递增值 . ID值是[2,4],但不是[1,2],因为它应该......这是xslt:

<xsl:template match="/Header/URLList/URLItem[not(child::ID)]">
    <xsl:copy>
      <ID> <xsl:value-of select="position()"/></ID>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

我也一直试图使用这样的for-each循环:

<xsl:template match="/Header/URLList">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    <xsl:for-each select="/Header/URLList/URLItem">
        <xsl:if test="not(ID)">
          <xsl:element name="ID"><xsl:value-of select="position()" /></xsl:element>
        </xsl:if>
    </xsl:for-each>
  </xsl:template>

这样我似乎得到了正确的增量,但新的ID元素出现在父节点上 . 我无法找到将它们作为URLItem元素的子元素附加的方法 .

任何帮助非常感谢 .

2 回答

  • 0

    代替

    <xsl:template match="/Header/URLList/URLItem[not(child::ID)]">
        <xsl:copy>
          <ID> <xsl:value-of select="position()"/></ID>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    

    使用

    <xsl:template match="/Header/URLList/URLItem[not(child::ID)]">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <ID><xsl:number/></ID>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
    
  • 3
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="/">
    <Header>
    <xsl:apply-templates select="//URLList"/>
    </Header>
    </xsl:template>
    <xsl:template match="URLList">
    <URLList>
    <xsl:for-each select="URLItem[not(child::ID)]">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
          <ID> <xsl:value-of select="position()"/></ID>
          <xsl:copy-of select="URL"/>
        </xsl:copy>
      </xsl:for-each>
      </URLList>
      </xsl:template>
      <xsl:template match="@*">
    <xsl:copy-of select="."/>
      </xsl:template>
    </xsl:stylesheet>
    

相关问题