Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 枚举XSLT节点列表中的项_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

Xml 枚举XSLT节点列表中的项

Xml 枚举XSLT节点列表中的项,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我有这样一些xml: <people> <person id="7" name="Arthur"> <person id="82" name="Lancelot"> <person id="14" name="Guinevere"> </people> XSLT不需要看起来像那样。如果有一个解决方案使用了或者需要在变量中使用循环,或者类似的东西,就像我在输入问题时经常发生的那样,我发现了解决方案。我想我会把它贴

我有这样一些xml:

<people>
    <person id="7" name="Arthur">
    <person id="82" name="Lancelot">
    <person id="14" name="Guinevere">
</people>
XSLT不需要看起来像那样。如果有一个解决方案使用了
或者需要在变量中使用循环,或者类似的东西,

就像我在输入问题时经常发生的那样,我发现了解决方案。我想我会把它贴在这里,以帮助其他可能遇到类似问题的人


您只需找到与您的id匹配的节点,然后使用前面的同级节点计数前面的所有节点。加1,你就有你的位置了

<xsl:variable name="positionOfPerson" select="count(/people/person[@id = $idToCheck]/preceding-sibling::*) + 1" />
<xsl:value-of select="$positionOfPerson" />

我发现在
count()
中使用
前面的
/
前面的同级
轴有时比使用
xsl:number
之类的方法进行计数要慢

例如

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="idToCheck" select="'82'"/>

  <xsl:template match="/people">
    <xsl:variable name="positionOfPerson">
      <xsl:apply-templates select="person[@id=$idToCheck]" mode="pos"/>
    </xsl:variable>
    <xsl:value-of select="$positionOfPerson"/>
  </xsl:template>

  <xsl:template match="*" mode="pos">
    <xsl:number/>
  </xsl:template>

</xsl:stylesheet>

我会这样做:

<people>
    <person id="7" name="Arthur">
    <person id="82" name="Lancelot">
    <person id="14" name="Guinevere">
</people>
在列表上递归迭代,直到找到要搜索的元素。 为当前迭代的位置使用一个参数

<xsl:variable name="idToCheck" select="'82'" />
<xsl:variable name="position">
<xsl:call-tamplate name="getPos">
<xsl:with-param name="pList" select="insert-parent-node-selector"/>
<xsl:with-param name="pElement" select="insert-specific-person-node-selector"/>
</xsl:call-template>
</xsl:variable>

<xsl:template name="getPos">
<xsl:param name="pElement" select="."/>
<xsl:param name="pList"/>
<xsl:param name="pNumber">1</xsl:param>


<xsl:choose>
<xsl:when test="pElement = pList[1]">
<xsl:value-of slect="pNumber">
</xsl:when>
<xsl:otherwise>
<xsl:call-tamplate name="getPos">
<xsl:with-param name="pList" select="pList[position() > 1]"/>
<xsl:with-param name="pElement"pElement"/>
<xsl:with-param name="pNumber"pNumber + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>

</xsl:template>

1.

我不认为
做了你认为它做的事情。@michael.hor257k mhh可能是,我不确定它是否也会以这种方式比较属性。但是,通过选择属性id而不是节点本身,可以很容易地解决这个问题。如果我错了,请纠正我。我不是在谈论属性与元素。我说的是比较节点的字符串值(您正在做)和比较节点的标识(我相信您认为您正在做),这确实是我认为我正在做的。谢谢你的提醒/node()应该解决这个问题,对吗?不。您需要比较节点的ID或测试它们的计数是否返回1。请将您的答案标记为已接受的答案,以便人们知道问题已得到回答。就我个人而言,我经常寻找未回答的问题,当我注意到一个问题在思考了几分钟后就得到了回答时,真的很烦人。@RobertS。很抱歉。我想看看人们是否有其他的解决方案,结果是,在这种情况下。我现在已经把它标记为已接受。谢谢你的提醒。
<xsl:variable name="idToCheck" select="'82'" />
<xsl:variable name="position">
<xsl:call-tamplate name="getPos">
<xsl:with-param name="pList" select="insert-parent-node-selector"/>
<xsl:with-param name="pElement" select="insert-specific-person-node-selector"/>
</xsl:call-template>
</xsl:variable>

<xsl:template name="getPos">
<xsl:param name="pElement" select="."/>
<xsl:param name="pList"/>
<xsl:param name="pNumber">1</xsl:param>


<xsl:choose>
<xsl:when test="pElement = pList[1]">
<xsl:value-of slect="pNumber">
</xsl:when>
<xsl:otherwise>
<xsl:call-tamplate name="getPos">
<xsl:with-param name="pList" select="pList[position() > 1]"/>
<xsl:with-param name="pElement"pElement"/>
<xsl:with-param name="pNumber"pNumber + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>

</xsl:template>