如何将一个foreach的变量数据复制到另一个xslt中的每个变量 [英] how to copy the data of variable of one foreach to another for each in xslt

查看:26
本文介绍了如何将一个foreach的变量数据复制到另一个xslt中的每个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

XSL

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

          <xsl:output method="text" />
           <xsl:param name="count" select="000001"></xsl:param >
            <xsl:template match="/">
             <xsl:for-each select ="swift/message">

             <xsl:variable name="newtype">
        <xsl:choose>
        <xsl:when test="block2[@type = 'input']">

     <xsl:value-of  select=" concat('O', block2/messageType,block2/messagePriority )"/>,<xsl:text/>
            </xsl:when>

            <xsl:when test="block2[@type = 'output']">
     <xsl:value-of  select=" concat('I', block2/messageType,block2/messagePriority )"/>,<xsl:text/>
        </xsl:when>
           </xsl:choose>
            </xsl:variable>

    <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($count,',',$first-val, ',',value)"/>

    <xsl:text>
        </xsl:text>
         </xsl:for-each>
       </xsl:for-each>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我需要复制我已声明名称为newtype"的数据,要求数据应打印在此波纹管的位置

i need to copy the data of which were i have declared name of "newtype" required the data should print in place of this bellow line

  <xsl:value-of select="concat($newtype,',',$first-val, ',',value)"/>

但上面显示错误,因为变量名被声明在范围之外,所以任何修改都可以让我达到那个输出

but above which was showing wrong because variable name was declared out of the scope so can any modify make me to reach that ouput please

上面我已经热编码了这个值 000001 但需要为每条记录增加

the above i have hot coded this value 000001 but needed increment for every record

预期输出

O102N,000001,praveen,pravz,USA

O102N,000002, praveen,pubbypravz,UK

推荐答案

你能告诉我你的输入 xml 和想要的输出 xml 吗?

Can you show me your input xml and desired output xml?

当我看到 xsl 中的 foreach 时,我有点畏缩——它是一种模板语言,很少需要 foreach...

I kind of cringe a little when I see a foreach in xsl - it's a template language, and rarely needs foreach...

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet [
    <!ENTITY comma "<xsl:text>,</xsl:text>">
    <!ENTITY cr "<xsl:text>
</xsl:text>">
]>
<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:apply-templates select="../../block2/@type"/>
        <xsl:value-of select="../../block2/messageType"/>
        <xsl:value-of select="../../block2/messagePriority"/>&comma;
        <xsl:number format="000001"/>&comma;
        <xsl:value-of select="../../block3/tag [name='32']/value"/>&comma;
        <xsl:value-of select="value"/>&cr;
    </xsl:template>

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

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

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

</xsl:stylesheet>

每个 block4 名称需要一行.因此,为该 block4/tag [name='77']

You need one row for each block4 name. So apply a template for that block4/tag [name='77']

然后 - 对于每一个,选择您需要的父元素.

Then - for every one of those, select the parent elements that you need.

xsl:number 将计算它选择的次数.

xsl:number will count the number of times it selected.

实体项目用于控制空格 - 否则格式是垃圾.

The ENTITY items are there to control whitespace - otherwise the formatting is crap.

不需要 foreach.希望这有帮助

No need for a foreach. Hope this helps

这篇关于如何将一个foreach的变量数据复制到另一个xslt中的每个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆