我有一个能够工作的XSL IF语句,我可能会通过编写多个XSL IF语句来实现我所需要的,但我想问是否有一种方法可以使用XSL if来做类似于SQL功能的事情。
在我的数据中,字段sce_moac.sce.srs可能是大约20个数值之一。我想使用XSL将这20个值转换为XML中保存的三个值之一(crs_udf5.crs.srs、crs_udf6.crs.srs、crs_udf7.crs.srs)。
XML
<exchange>
<sce>
<sce.srs>
<sce_scjc.sce.srs>560021325/2</sce_scjc.sce.srs>
<sce_seq2.sce.srs>06</sce_seq2.sce.srs>
<sce_moac.sce.srs>01</sce_moac.sce.srs>
<crs>
<crs.srs>
<crs_udf5.crs.srs>114</crs_udf5.crs.srs>
<crs_udf6.crs.srs>115</crs_udf6.crs.srs>
<crs_udf7.crs.srs>118</crs_udf7.crs.srs>
</crs.srs>
</crs>
</sce.srs>
</sce>
</exchange>XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" media-type="text/x-json"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/">
<xsl:text>[</xsl:text>
<xsl:for-each select="/exchange/sce/sce.srs/sce_seq2.sce.srs">
<xsl:text> {PATRONCODE":"</xsl:text>
<xsl:if test="..//sce_moac.sce.srs=01">
<xsl:value-of select="../../..//crs_udf5.crs.srs"/>
</xsl:if>
<xsl:text>"</xsl:text>
<xsl:text>}</xsl:text>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:template>
</xsl:stylesheet>我想要做的是有一个值列表,而不是xsl中的一个值:如果测试是这样而不是<xsl:if test="..//sce_moac.sce.srs=01">,那么我将有一个布尔OR,所以更像这个<xsl:if test="..//sce_moac.sce.srs=01|02|43">,但是这个语法是无效的。有办法这样做吗?
发布于 2022-06-13 14:04:34
XPath/XSLT2.0(及以上)
可以指定要对其进行测试的值序列:
<xsl:if test="..//sce_moac.sce.srs=('01','02','43')">XPath/XSLT1.0
通常会将值分解为单独的逻辑或条件:
<xsl:if test="..//sce_moac.sce.srs='01' or
..//sce_moac.sce.srs='02' or
..//sce_moac.sce.srs='43')">请注意,在应用上述模式时,您可能希望在变量中捕获LHS,而不是重复XPath。
Dimitre还有一个clever trick (+1),如果您必须使用XSLT1.0,您可能希望在这里应用它。
https://stackoverflow.com/questions/72603816
复制相似问题