英文:
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:
<start>
<Item>
<ItemName>x</ItemName>
<ItemValue>x val</ItemValue>
</Item>
<Item>
<ItemName>y</ItemName>
<ItemValue>y val</ItemValue>
</Item>
<Item>
<ItemName>z</ItemName>
<ItemValue>z val</ItemValue>
</Item>
<Item>
<ItemName>y</ItemName>
<ItemValue>y val</ItemValue>
</Item>
<Item>
<ItemName>Head</ItemName>
<ItemValue>Head val</ItemValue>
</Item>
<Item>
<ItemName>Head1</ItemName>
<ItemValue>Head1 Val</ItemValue>
</Item>
<Item>
<ItemName>Head2</ItemName>
<ItemValue>Head2 Val</ItemValue>
</Item>
</start>
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
<xsl:if test="/start/Item[ItemName='Head'] "/>
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
<fo:block>
<xsl:template match="start/Item[ItemName='Head']">
Count <xsl:value-of select="count(preceding-sibling::Item)"/>
</xsl:template>
</fo:block>
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:
</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/ItemItemName='Head']">
<xsl:value-of select="count(preceding-sibling::Item)"/>
</xsl:template>
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
在这个上下文中,例如<xsl:template match="start/Item[ItemName = 'Head']">
,您可以选择例如count(preceding-sibling::Item)
来查找当前匹配的Item
之前有多少个Item
元素作为兄弟元素。
当然,根据您的需求和/或上下文,您可能希望例如count(/start/Item[ItemName = 'Head']/preceding-sibling::Item)
或(XPath 2及更高版本)/start/Item[ItemName = 'Head']/count(preceding-sibling::Item)
。
英文:
In the context of e.g. <xsl:template match="start/Item[ItemName = 'Head']">
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 = 'Head']/preceding-sibling::Item)
or (XPath 2 and later) /start/Item[ItemName = 'Head']/count(preceding-sibling::Item)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论