fetching data from xml using xslt

2019-09-10 10:24发布

问题:

having a xml with same tag names but for that tag names values were different , so we expecting an output like one by one transaction

<swift>
 <message>
 <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>76</name>
         <value>shanmu</value>
   </tag>
   </block4>
  </message>
</swift>

xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">            
    <xsl:for-each select="swift/message">   

                  <xsl:for-each select ="block3/tag[name = '32']">
            <xsl:value-of select="value"/>
        </xsl:for-each>,<xsl:text/>

                    <xsl:for-each select ="block4/tag[name = '77']">
            <xsl:value-of select="value"/>,<xsl:text/>
        </xsl:for-each>

        </xsl:for-each>

</xsl:template>

by this above xslt i have reached up to this

praveen,pravz,pubbypravz,

output needed:

 praveen,pravz

 praveen,pubbypravz

hope we need to set a loop for each time please guide me ...

回答1:

Well, you changed your first example completely, so my first answer did not match any more to your question. That makes our discussion some kind of worthless for outsiders. Nevertheless, I adapted my solution to your new input data:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />

    <xsl:template match="/">
    <xsl:for-each select ="/swift/message/block3/tag[name='32']">
       <xsl:variable name = "first-val" select="value"/>
       <xsl:for-each select ="/swift/message/block4/tag[name='77']">
           <xsl:value-of select="concat($first-val, ',',value)"/>
<xsl:text>
</xsl:text>
       </xsl:for-each>
    </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

Hope this helps.



标签: xml xslt