如何在xslt中将一个foreach的变量数据复制到另一个foreach

发布于 2024-11-09 15:05:48 字数 2905 浏览 0 评论 0原文

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”的数据,要求数据应打印在以下行的位置,

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

但上面显示错误,因为变量名称被声明为范围,因此任何修改都可以使我达到该输出,请

上面我已热编码该值 000001,但每个记录都需要增量

预期输出

O102N,000001,praveen,pravz,USA

O102N ,000002, praveen,pubbypravz,英国

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>

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

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

Expected Output

O102N,000001,praveen,pravz,USA

O102N,000002, praveen,pubbypravz,UK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白首有我共你 2024-11-16 15:05:48

你能告诉我你的输入 xml 和所需的输出 xml 吗?

当我在 xsl 中看到 foreach 时,我有点畏缩 - 它是一种模板语言,很少需要 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"/>,
        <xsl:number format="000001"/>,
        <xsl:value-of select="../../block3/tag [name='32']/value"/>,
        <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'] 应用一个模板,

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

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

ENTITY 项用于控制空白 - 否则格式很糟糕。

不需要foreach。希望这有帮助

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

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"/>,
        <xsl:number format="000001"/>,
        <xsl:value-of select="../../block3/tag [name='32']/value"/>,
        <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>

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 will count the number of times it selected.

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

No need for a foreach. Hope this helps

小伙你站住 2024-11-16 15:05:48

XSLT 是一种函数式语言 - 这意味着变量是不可变的 - 一旦给定值,它们就无法更改。

此特定问题的解决方案

更改

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

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

当将更正的转换应用于提供的 XML 文档时,所需的结果已生成

XSLT is a functional language -- among other things this means that variables are immutable -- once given a value they cannot be changed.

The solution to this specific problem:

Change:

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

To:

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

When the corrected transformation is applied to the provided XML document, the wanted result is produced.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文