[英]XQuery transform multiple xml elements into one target element concatenated
[英]How to conditionally transform multiple XML elements into one element using XSLT?
假设我有两种XML元素,其文本内容可以是'true'或'false':
<elementA>true</elementA>,
<elementB>false</elementB>
我想创建一个新元素
<result>$content</result>
其中,变量$ content可以基于两个输入元素值具有4种类型的值:
正确,正确-> A
正确,错误-> B
假,真-> C
错误,错误-> D
我试图用xsl:function解决这个问题,但是不知道如何同时选择两个元素(我对xslt的经验很少)。 可以进行这种转换吗?
编辑
我正在尝试使用您的第二个解决方案,但是,问题的主要根源在于我不知道如何将变量传递给函数。 目前,我有以下代码:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:function name="transform">
<xsl:param name="p1"></xsl:param>
<xsl:param name="p2"></xsl:param>
<xsl:choose>
<xsl:when test="$p1='true' and $p2='true'">
<orderingType>RND</orderingType>
</xsl:when>
<xsl:when test="$p1='true' and $p2='false'">
<orderingType>FIX</orderingType>
</xsl:when>
<xsl:when test="$p1='false' and $p2='false'">
<orderingType>PREF</orderingType>
</xsl:when>
</xsl:choose>
</xsl:function>
<xsl:template match="//action/*">
<xsl:variable name="var1" select="shuffle"></xsl:variable>
<xsl:variable name="var2" select="limitOn"></xsl:variable>
</xsl:template>
怎么样:
<xsl:variable name="index" select="2*(elementA='false') + (elementB='false')" />
<result>
<xsl:value-of select="substring('ABCD', $index + 1, 1)" />
</result>
或者,使用xsl:choose
并在xsl:when
指令中明确指定每种情况及其结果:
<xsl:choose>
<xsl:when test="elementA='true' and elementB='true'">A</xsl:when>
<xsl:when test="elementA='true' and elementB='false'">B</xsl:when>
<xsl:when test="elementA='false' and elementB='true'">C</xsl:when>
<xsl:otherwise>D</xsl:otherwise>
</xsl:choose>
模板应该做到这一点;
<xsl:template match="xml-fragment">
<xsl:variable name="p1">
<xsl:value-of select="elementA"/>
</xsl:variable>
<xsl:variable name="p2">
<xsl:value-of select="elementB"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$p1='true' and $p2='true'">
<orderingType>RND</orderingType>
</xsl:when>
<xsl:when test="$p1='true' and $p2='false'">
<orderingType>FIX</orderingType>
</xsl:when>
<xsl:when test="$p1='false' and $p2='false'">
<orderingType>PREF</orderingType>
</xsl:when>
</xsl:choose>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.