且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-24 15:23:53

你能告诉我你的输入 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