如何通过定位特定标签来获取其他标签 [英] how to fetch the other tags by targeting a particular tag
问题描述
这里我粘贴了一个 XML 示例,它是标签名称,如 21A,50F,21D,22B
.
Here I have pasted a sample of XML that was tag names like 21A,50F,21D,22B
.
通常,如果我需要获取特定标签,我可以在 XSLT 中轻松使用以下逻辑:
Normally if I need to fetch a particular tag, I can use logic below easily in XSLT:
<xsl:choose>
<xsl:when test="tag[name = '21A'] ">
<xsl:choose>
<xsl:when test="substring(tag[name = '21A']/value,1,1) = '/'">
<xsl:variable name="result" select="concat(translate(tag[name = '21A']/value,',',' '),' ')"/>
<xsl:value-of select="substring(substring-before(substring-after($result,' '),' '),1,11)"/>
</xsl:when>
<xsl:when test="substring(tag[name = '21A']/value,1,1) != '/'">
<xsl:value-of select="substring(tag[name = '21A']/value,1,11)"/>
</xsl:when>
</xsl:choose>
</xsl:when>
</xsl:choose>
但是这里我有一个要求为 Sequence A
和 Sequence B
.
but here I have a requirement as Sequence A
and Sequence B
.
- 填充在
50F
上方的标签位于sequence A
- 填充在
50F
以下的标签属于sequence B
- the tags which populated above
50F
come undersequence A
- the tags which populated below
50F
come undersequence B
因为我们需要基于使用 50F
标签来获取标签.谁能给个建议?
since we need to fetch the tags based on using 50F
tag. Can any one please give a suggestion?
<local>
<message>
<block4>
<tag>
<name>21A</name>
<value>ALW1031</value>
</tag>
<tag>
<name>50F</name>
<value>TESTING CITI BANK EFT9</value>
</tag>
<tag>
<name>21D</name>
<value>OUR</value>
</tag>
<tag>
<name>22B</name>
<value>ipubby</value>
</tag>
</block4>
</message>
</local>
需要输出:
ALW1031,OUR
以前如果假设他们已经填充 21A
两次意味着我在调用标签值时使用了 [1] 和 [2] 位置.现在他们将重复填充 21 个标签,但标签可能是 A
或 D
所以我需要盲目地定位 50f
标签.无论他们提供什么标签,在 50F
之前的 A
或 D
我都需要类似地获取他们在 50F
之后填充的标签> 我们能够获取如此避免位置.
Previously if suppose they have populate 21A
two times means I have used the position as [1] and [2] as while calling tag values. Now they will populate 21 tag repeatedly but tags may be A
or D
so I need to target 50f
tag blindly. Whatever tag they will provide, either A
or D
before 50F
I need to fetch similarly whatever they populate tags after 50F
we able to fetch so avoiding positions.
总结:@Treemonkey:希望您对我的示例 XML 一目了然.它有一些标签,如 21A,50F
等等.假设如果我有两个字段 field1,field2
之前它们填充了与 21A
相同的重复标签,当时我已经提取为 21A
有旁边标记字段 1 的位置 [1] (tag[name = '21A'][1]
)
Summary:
@Treemonkey: hope you had a glance of my sample XML. It has some tag like 21A,50F
and so on. Assume if I have two fields field1,field2
earlier they have populated tags as same repeated tags as 21A
at that time I have fetched as 21A
having beside by marking position [1] for field 1 (tag[name = '21A'][1]
)
类似地,21A
除了标记字段 2 的位置 [2],现在它们将填充 21
但标签与 A
或D
.正如我所说的 field1
应该集中 sequence A
和 field2
应该集中在 sequence B
所以现在我们不应该打扰关于获取的位置,我们有一个像标记 50F
一样的分界线,在 50F
之前和 50F 之后将填充的任何字段都必须被视为
sequence A
必须被视为 sequence B
.
Similarly 21A
having beside by marking position [2] for field 2, now they will populate 21
but tags were different as A
or D
. As I have said field1
should concentrate sequence A
and field2
should concentrate as sequence B
so now we should not bother about positions for fetch we have a demarcation like tag 50F
whatever fields will populate before 50F
has to treated as sequence A
and after 50F
has to be treated as sequence B
.
所以最后我们需要通过针对 50
F 来编写 XSLT.如果我想在 50F
之前的(示例 XML)中显示 21A
字段,那么我们需要在 XSLT 中编写一个逻辑作为 21A
之前的选择标签 21A
code>50F 标记用于在字段 1 和字段 2 中生成数据,我们需要在 50F
之后获取为 21D
所以我们需要编写一个逻辑作为选择21D
在 50F
之后.
So finally we need to write as XSLT by targeting 50
F. If I want to display 21A
field in (sample XML) which before 50F
so we need write a logic in XSLT as select tag 21A
before 50F
tag for to produce data in field 1 and for field 2 we need to fetch as 21D
after 50F
so we need to write a logic as select 21D
after 50F
.
推荐答案
您的要求不是很清楚,但以下表达应该会有所帮助.
Your requirements aren't exactly clear, but the following expressions should help.
选择 tag
之前的所有兄弟元素,其子元素 name
具有特定值:
Select all siblings before the tag
whose child name
has a particular value:
/*/*/*/tag[name='50F']/preceding-sibling::*
选择以下兄弟姐妹:
/*/*/*/tag[name='50F']/following-sibling::*
只选择前一个元素:
/*/*/*/tag[name='50F']/preceding-sibling::*[1]
选择具有特定值的孩子 name
的前面兄弟姐妹:
Select preceding siblings having a child name
with a particular value:
/*/*/*/tag[name='50F']/preceding-sibling::*[name='21A']
选择具有特定值的子name
的以下兄弟姐妹:
Select following siblings having a child name
with a particular value:
/*/*/*/tag[name='50F']/following-sibling::*[name='21D']
这篇关于如何通过定位特定标签来获取其他标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!