how to check two or more conditions in xsl templat

2019-08-02 20:50发布

how to check two or more conditions in xslt

here is my xml

 <swift>
 <message>
 <block2 type="input">
    <messageType>102</messageType>
    <receiverAddress>BKTRUS33XBRD</receiverAddress>
    <messagePriority>N</messagePriority>     
 </block2>
 <block3>
 <tag>
 <name>32</name>
 <value>praveen</value>
 </tag>
 <tag>
 <name>42</name>
 <value>pubby</value>
 </tag>
 </block3> 
 <block4>
 <tag>
 <name>77</name>
 <value>pravz</value>
 </tag>
 <tag>
 <name>77</name>
 <value>pubbypravz</value>
 </tag>
 <tag>
 <name>99</name>
 <value>USA</value>
 </tag>
 <tag>
 <name>99</name>
 <value>UK</value>
 </tag>
 <tag>
 <name>76</name>
 <value>shanmu</value>
 </tag>
</block4>
 </message>
 </swift>

for this above xml we have applying this below xsl template here if any one of tag repeatation is occur xslt were working if suppose another tag were repeatation happen in xml means how can apply a logic in xslt

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
 >
<xsl:output method="text" indent="no" />
<xsl:template match="/">
    <xsl:apply-templates select="/swift/message/block4/tag [name='77']"/>
</xsl:template>
<xsl:template match="/">
    <xsl:apply-templates select="/swift/message/block4/tag [name='99']"/>
</xsl:template>

<xsl:template match="message/block4/tag [name='77']">
    <xsl:apply-templates select="../../block2/@type"/>
    <xsl:value-of select="../../block2/messageType"/>
    <xsl:value-of select="../../block2/messagePriority"/>,<xsl:text/>
    <xsl:number format="000001"/>,<xsl:text/>
    <xsl:value-of select="../../block3/tag [name='32']/value"/>,<xsl:text/>
    <xsl:value-of select="value"/>

</xsl:template>

<xsl:template match="message/block4/tag [name='99']">


    <xsl:value-of select="value"/>
    <xsl:text>
    /xsl:text>
</xsl:template>

<xsl:template match="@type[.='input']">O</xsl:template>

<xsl:template match="@type[.='output']">I</xsl:template>

<xsl:template match="text()"/>

  </xsl:stylesheet>

Expected Output

O102N,000001,praveen,pravz,USA

O102N,000002, praveen,pubbypravz,UK

标签: xslt xslt-1.0
1条回答
姐就是有狂的资本
2楼-- · 2019-08-02 21:37

To help you learn by example, I modified your transformation just a little:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" >
    <xsl:output method="text" indent="no" />
    <xsl:template match="/">
        <xsl:apply-templates select="/swift/message/block4/tag [name='77']"/>
    </xsl:template>
    <xsl:template match="message/block4/tag [name='77']">
        <xsl:variable name="position" select="position()"/>
        <xsl:apply-templates select="../../block2/@type"/>
        <xsl:value-of select="../../block2/messageType"/>
        <xsl:value-of select="../../block2/messagePriority"/>,<xsl:text/>
        <xsl:number format="000001"/>,<xsl:text/>
        <xsl:value-of select="../../block3/tag [name='32']/value"/>,<xsl:text/>
        <xsl:value-of select="value"/>,<xsl:text/>
        <xsl:apply-templates select="/swift/message/block4/tag [name='99'][position()=$position]"/>
    </xsl:template>
    <xsl:template match="message/block4/tag [name='99']">
        <xsl:value-of select="value"/>
        <xsl:text>
</xsl:text>
    </xsl:template>
    <xsl:template match="@type[.='input']">O</xsl:template>
    <xsl:template match="@type[.='output']">I</xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

When applied to this document:

 <swift>
    <message>
        <block2 type="input">
            <messageType>102</messageType>
            <receiverAddress>BKTRUS33XBRD</receiverAddress>
            <messagePriority>N</messagePriority>
        </block2>
        <block3>
            <tag>
                <name>32</name>
                <value>praveen</value>
            </tag>
            <tag>
                <name>42</name>
                <value>pubby</value>
            </tag>
        </block3>
        <block4>
            <tag>
                <name>77</name>
                <value>pravz</value>
            </tag>
            <tag>
                <name>77</name>
                <value>pubbypravz</value>
            </tag>
            <tag>
                <name>99</name>
                <value>USA</value>
            </tag>
            <tag>
                <name>99</name>
                <value>UK</value>
            </tag>
            <tag>
                <name>76</name>
                <value>shanmu</value>
            </tag>
        </block4>
    </message>
</swift>

It produces the following result:

O102N,000001,praveen,pravz,USA
O102N,000002,praveen,pubbypravz,UK
查看更多
登录 后发表回答