how to fetch the other tags by targeting a particu

2019-08-29 06:01发布

Here I have pasted a sample of XML that was tag names like 21A,50F,21D,22B.

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,',',' '),'&#13;')"/>
                <xsl:value-of select="substring(substring-before(substring-after($result,'&#13;'),'&#13;'),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>

but here I have a requirement as Sequence A and Sequence B.

  • the tags which populated above 50F come under sequence A
  • the tags which populated below 50F come under sequence B

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>

output required:

ALW1031,OUR

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.


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])

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.

So finally we need to write as XSLT by targeting 50F. 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.

标签: xslt xslt-1.0
1条回答
三岁会撩人
2楼-- · 2019-08-29 06:36

Your requirements aren't exactly clear, but the following expressions should help.

Select all siblings before the tag whose child name has a particular value:

/*/*/*/tag[name='50F']/preceding-sibling::*

Select following siblings:

/*/*/*/tag[name='50F']/following-sibling::*

Select just the immediately preceding element:

/*/*/*/tag[name='50F']/preceding-sibling::*[1]

Select preceding siblings having a child name with a particular value:

/*/*/*/tag[name='50F']/preceding-sibling::*[name='21A']

Select following siblings having a child name with a particular value:

/*/*/*/tag[name='50F']/following-sibling::*[name='21D']
查看更多
登录 后发表回答