不满足特定条件时如何删除标题并不产生任何输出 [英] how to remove headers and produce no output when specific condition is not met

查看:90
本文介绍了不满足特定条件时如何删除标题并不产生任何输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用XSLT从XML转换为输出csv格式文本的XML数据.当满足过滤条件时,它将生成输出.现在,当不满足过滤条件时,我需要代码停止产生任何输出.当前,当不满足过滤条件时,它将生成标头.

I have XML data which I have transformed using XSLT from XML to output csv formatted text. It generates the output when the filter condition is met. I now need the code to stop producing any output when filter condition not met. Currently it generates header when the filter condition is not met.

我尝试设置全局变量,但出现错误.

I tried setting global variable but getting an error.

<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="delimiter" select="','"/>
<xsl:param name="xslt.transform.params"></xsl:param>
<xsl:variable name="countryCode">
    <xsl:value-of select="$xslt.transform.params"/>
</xsl:variable>
<csv:columns>
    <column>ORIG</column>
    <column>CUST</column>
    <column>PARTY</column>
    <column>ACCOUNT1</column>
    <column>ACCOUNT2</column>
    <column>CURRENCY</column>
    <column>SUM1</column>
    <column>SUM2</column>
    <column>SUM3</column>
    <column>SUM4</column>
    <column>SUM5</column>
    <column>SUM6</column>
    <column>SUM7</column>
    <column>SUM8</column>
    <column>SUM9</column>
    <column>SUM10</column>
    <column>SUM11</column>
    <column>SUM12</column>
</csv:columns>
<xsl:template match="/">
    <xsl:for-each select="document('')/*/csv:columns/*">
        <xsl:value-of select="."/>
        <xsl:if test="position() != last()">
            <xsl:value-of select="$delimiter"/>
        </xsl:if>
    </xsl:for-each>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="/DATA/G">
    <xsl:variable name="result" select="."/>
    <xsl:variable name="orgsysref" select="ORIG"/>
    <xsl:for-each select="document('')/*/csv:columns/*">
        <xsl:variable name="vCountry" select="fn:substring($orgsysref,1,2)"/>
        <xsl:if test="$vCountry = fn:substring-before($countryCode,'|') or $vCountry = fn:substring-after($countryCode, '|') or $vCountry = $countryCode">
            <xsl:variable name="column" select="."/>
            <xsl:variable name="value" select="$result/*[name() = $column]"/>
            <xsl:value-of select="$value"/>
            <xsl:if test="position() != last()">
                <xsl:value-of select="$delimiter"/>
            </xsl:if>
        </xsl:if>
    </xsl:for-each>
    <xsl:text>&#xa;</xsl:text>
</xsl:template>

我尝试添加全局变量.

但出现错误:与所需序列类型匹配的错误出现

but getting an error: Wrong occurrence to match required sequence type

<xsl:template match="/">
        <xsl:variable name="sysref" select="/DATA/G/ORIG"/>
        <xsl:variable name="country">
            <xsl:value-of select="fn:substring($sysref,1,2)"/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="$countryCode != '' and $country = $countryCode">
                <xsl:for-each select="document('')/*/csv:columns/*">
                    <xsl:value-of select="."/>
                    <xsl:if test="position() != last()">
                        <xsl:value-of select="$delimiter"/>
                    </xsl:if>
                </xsl:for-each>
                <xsl:text>&#xa;</xsl:text>
                <xsl:apply-templates select="*"/>

国家/地区代码不匹配时,不应生成任何输出.它与输出匹配时应为:

There should not be any output generated when country code doesnt match. when it matches the output should be:

test107015196,300000011997776,preet,0432007015196,preet,EUR,440.26,3,0,0,16.66,1,423.6,2,0,0,0,0
test100330125,300000013609676,Margarethe Krögner,0432000330125,Margarethe Krögner,EUR,88.73,1,0,0,88.73,1,0,0,0,0,0,0
test107015561,300000013996996,smith,0432007015561,smith,EUR,239.94,1,0,0,239.94,1,0,0,0,0,0,0

不匹配时,无输出.

请帮助.谢谢.

推荐答案

变量可以简单地具有所需的谓词,例如

A variable could simply have the required predicate e.g.

<xsl:variable name="data-to-be-processed" select="/DATA/G[substring(ORIG,1,2) = $countryCode]"/>

,然后在需要的地方使用它,例如

and then you use it where needed with e.g.

<xsl:if test="$data-to-be-processed">
  <!-- output header -->
  ...
  <xsl:apply-templates select="$data-to-be-processed"/>
</xsl:if>

这篇关于不满足特定条件时如何删除标题并不产生任何输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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