查找元素的数量,其中元素由XML中标签的内容文本标识。

huangapple go评论65阅读模式
英文:

Find The Count Of An Element Where Element Is Getting Identified By The Content Text Of Tag In The XML

问题

I will only provide translations of the content you've provided without additional information. Here's the translation:

我正在尝试查找父元素的位置或计数;当我们能够通过包含文本来识别其子元素时;以下是XML:

现在,一旦我找到/start/Item[ItemName='Head'],我想找出它的位置或在此之前有多少'/start/Item',以便我可以使用该计数/值/位置并获取更多的Heads Head1、Head2等...;
ItemName Head将是静态的,但Head1、Head2将是动态的。

我已经使用以下方法成功遍历到ItemName Head:

<xsl:if test="/start/Item[ItemName='Head']" />

那么在此之后如何实现目标呢?

更新

我尝试了以下代码:

只是尝试在正文中打印计数,但它不起作用

fo:block
<xsl:template match="start/Item[ItemName='Head']">
Count <xsl:value-of select="count(preceding-sibling::Item)"/>
</xsl:template>
</fo:block>
我是否漏掉了什么?

我是新手,所以如果我在这里犯了很基本的错误,我向您道歉

---------更新--------

抱歉因不便添加了评论中的代码并将其更新到问题中。所以正如建议的那样,我尝试了它,是的,它起作用;以下是有效的代码:

</xsl:template>
<xsl:variable name="var">
<xsl:apply-templates select="start/Item[ItemName='Head']"/>
</xsl:variable>

<fo:block margin-top="9.50mm">
<xsl:value-of
select="start/Item[number($var)+1]/ItemValue"/>
</fo:block>
</xsl:template>

<xsl:template match="start/Item[ItemName='Head']">
<xsl:value-of select="count(preceding-sibling::Item)"/>
</xsl:template>

我们是否有其他获取XML元素索引的简便方法?只想知道我是否为获取索引而编写了不必要的代码?

英文:

I am trying Find The Position Or Count of a parent element ; when we can identify its child element with the help of it containing text; below is the xml:

&lt;start&gt;
&lt;Item&gt;
&lt;ItemName&gt;x&lt;/ItemName&gt;
&lt;ItemValue&gt;x val&lt;/ItemValue&gt;
&lt;/Item&gt;
&lt;Item&gt;
&lt;ItemName&gt;y&lt;/ItemName&gt;
&lt;ItemValue&gt;y val&lt;/ItemValue&gt;
&lt;/Item&gt;
&lt;Item&gt;
&lt;ItemName&gt;z&lt;/ItemName&gt;
&lt;ItemValue&gt;z val&lt;/ItemValue&gt;
&lt;/Item&gt;
&lt;Item&gt;
&lt;ItemName&gt;y&lt;/ItemName&gt;
&lt;ItemValue&gt;y val&lt;/ItemValue&gt;
&lt;/Item&gt;
&lt;Item&gt;
&lt;ItemName&gt;Head&lt;/ItemName&gt;
&lt;ItemValue&gt;Head val&lt;/ItemValue&gt;
&lt;/Item&gt;
&lt;Item&gt;
&lt;ItemName&gt;Head1&lt;/ItemName&gt;
&lt;ItemValue&gt;Head1 Val&lt;/ItemValue&gt;
&lt;/Item&gt;
&lt;Item&gt;
&lt;ItemName&gt;Head2&lt;/ItemName&gt;
&lt;ItemValue&gt;Head2 Val&lt;/ItemValue&gt;
&lt;/Item&gt;
&lt;/start&gt;

Now as soon as I will find /start/Item[ItemName='Head'] this, I want to find out what is it's position or how many '/start/Item' precedes before this ItemName='Head' so that I can use that count/value/position and fetch further Heads Head1, Head2 etc... ;
ItemName Head will be static but Head1, Head2 will be dynamic.

I checked using below I am able to travers to ItemName Head

&lt;xsl:if test=&quot;/start/Item[ItemName=&#39;Head&#39;] &quot;/&gt; 

So how I can achieve the goal after this ?

Update

I tried below code :

Just trying to print the count in the body but it is not working

&lt;fo:block&gt;
  &lt;xsl:template match=&quot;start/Item[ItemName=&#39;Head&#39;]&quot;&gt;
    Count &lt;xsl:value-of select=&quot;count(preceding-sibling::Item)&quot;/&gt; 
  &lt;/xsl:template&gt;
&lt;/fo:block&gt;

Am I missing some thing here?
I am new to this so my apology If I am doing very basic mistake here

---------Update--------

Sorry for being inconvenient that I added the code in comment and updated the same in the question. So as it was suggested I tried that and yes it is working ; below is the working code:

&lt;/xsl:template&gt;
 &lt;xsl:variable name=&quot;var&quot;&gt;
    &lt;xsl:apply-templates select=&quot;start/Item[ItemName=&#39;Head&#39;]&quot;/&gt;
&lt;/xsl:variable&gt;

 &lt;fo:block  margin-top=&quot;9.50mm&quot;&gt;
  &lt;xsl:value-of 
  select=&quot;start/Item[number($var)+1]/ItemValue&quot;/&gt;
 &lt;/fo:block&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;start/ItemItemName=&#39;Head&#39;]&quot;&gt;
  &lt;xsl:value-of select=&quot;count(preceding-sibling::Item)&quot;/&gt;
&lt;/xsl:template&gt;

Do we have any other short approach for retrieving the index of the xml element ; just wanted to know am I writing unnecessary code for just to get the index ?

答案1

得分: 0

在这个上下文中,例如&lt;xsl:template match=&quot;start/Item[ItemName = &#39;Head&#39;]&quot;&gt;,您可以选择例如count(preceding-sibling::Item)来查找当前匹配的Item之前有多少个Item元素作为兄弟元素。

当然,根据您的需求和/或上下文,您可能希望例如count(/start/Item[ItemName = &#39;Head&#39;]/preceding-sibling::Item)或(XPath 2及更高版本)/start/Item[ItemName = &#39;Head&#39;]/count(preceding-sibling::Item)

英文:

In the context of e.g. &lt;xsl:template match=&quot;start/Item[ItemName = &#39;Head&#39;]&quot;&gt; you can select e.g. count(preceding-sibling::Item) to find out how many Item elements precede the currently matched Item as siblings.

Of course depending on your needs and/or context you might want e.g. count(/start/Item[ItemName = &#39;Head&#39;]/preceding-sibling::Item) or (XPath 2 and later) /start/Item[ItemName = &#39;Head&#39;]/count(preceding-sibling::Item).

huangapple
  • 本文由 发表于 2023年4月11日 14:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定