问题
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>
</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>
</xsl:text>
</xsl:template>
I have tried adding global variable.
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>
</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
when doesn't match, no output.
Please help. Thank you.
回答1:
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>
来源:https://stackoverflow.com/questions/58696345/how-to-remove-headers-and-produce-no-output-when-specific-condition-is-not-met