基于参数的 XSLT 动态求和
XSLT dynamic Sum based on a parameter
我正在从 Apache Camel 调用此 XSL,并将消息的 header 设置为参数,但我仍然没有得到结果。
我想要一个 XSLT,它根据参数给出总和问题是参数可以是多个值或一个值
<EmpJob>
<EmpJob>
<userId>testID</userId>
<EmpPayCompRecurring>
<payComponent>1010</payComponent>
<endDate>2020-06-30T00:00:00.000</endDate>
<paycompvalue>3025.67</paycompvalue>
<userId>testID</userId>
<currencyCode>EUR</currencyCode>
<startDate>2020-06-01T00:00:00.000</startDate>
</EmpPayCompRecurring>
<EmpPayCompRecurring>
<payComponent>6097</payComponent>
<endDate>2019-12-31T00:00:00.000</endDate>
<paycompvalue>100.0</paycompvalue>
<userId>testID</userId>
<currencyCode>EUR</currencyCode>
<startDate>2018-12-06T00:00:00.000</startDate>
</EmpPayCompRecurring>
</EmpJob>
</EmpJob>
我创建了一个 XSLT 转换
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name = "custReturnDate" />
<xsl:template match="/EmpJob">
<xsl:variable name="apos">'</xsl:variable>
<xsl:variable name="payrecur">'1010','6097' </xsl:variable>
<mainroot>
<xsl:for-each select="EmpJob">
<root>
<__metadata>
<uri><xsl:value-of select="concat('EmpCompensation(seqNumber=1L,startDate=datetime',$apos ,$custReturnDate, $apos,',userId=',$apos,userId,$apos,')')"/></uri>
</__metadata>
<customDouble13><xsl:value-of select="sum(EmpPayCompRecurring[$payrecur]/paycompvalue) "/></customDouble13>
</root>
</xsl:for-each>
</mainroot>
</xsl:template>
</xsl:stylesheet>
“payrecur”应该是稍后根据这个参数我应该对节点值求和的参数。
P.S。我可以将参数更改为任何参数,因为我是从代码调用模板。
给定 XSLT 2 或 3,我会将参数声明为字符串或整数序列,然后比较 sum(EmpPayCompRecurring[payComponent = $payrecur]/paycompvalue)
。
所以声明例如<xsl:param name="payrecur" as="xs:string*" select="'1010','6097'"/>
。
最小样式表是
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:param name = "custReturnDate" />
<xsl:param name="payrecur" as="xs:string*" select="'1010','6097'"/>
<xsl:output indent="yes"/>
<xsl:template match="/EmpJob">
<xsl:variable name="apos">'</xsl:variable>
<mainroot>
<xsl:for-each select="EmpJob">
<root>
<__metadata>
<uri><xsl:value-of select="concat('EmpCompensation(seqNumber=1L,startDate=datetime',$apos ,$custReturnDate, $apos,',userId=',$apos,userId,$apos,')')"/></uri>
</__metadata>
<customDouble13>
<xsl:value-of select="sum(EmpPayCompRecurring[payComponent = $payrecur]/paycompvalue)"/>
</customDouble13>
</root>
</xsl:for-each>
</mainroot>
</xsl:template>
</xsl:stylesheet>
在 https://xsltfiddle.liberty-development.net/bEzkTcM 我得到 <customDouble13>3125.67</customDouble13>
。您可能希望在 xsl:stylesheet
元素上添加 exclude-result-prefixes="#all"
。
大家好,如果有人遇到我在 header 中添加参数以便 XSLT 模板获取它之前提到的这种情况。不幸的是,模板将它作为字符串接收,所以我不得不使用函数 tokenize() 将参数转换为数组,然后我可以使用 Martin Honnen 提供的模板。
此方案适用于 Apache Camel 和 SAP CPI
此致
易卜拉欣
我正在从 Apache Camel 调用此 XSL,并将消息的 header 设置为参数,但我仍然没有得到结果。
我想要一个 XSLT,它根据参数给出总和问题是参数可以是多个值或一个值
<EmpJob>
<EmpJob>
<userId>testID</userId>
<EmpPayCompRecurring>
<payComponent>1010</payComponent>
<endDate>2020-06-30T00:00:00.000</endDate>
<paycompvalue>3025.67</paycompvalue>
<userId>testID</userId>
<currencyCode>EUR</currencyCode>
<startDate>2020-06-01T00:00:00.000</startDate>
</EmpPayCompRecurring>
<EmpPayCompRecurring>
<payComponent>6097</payComponent>
<endDate>2019-12-31T00:00:00.000</endDate>
<paycompvalue>100.0</paycompvalue>
<userId>testID</userId>
<currencyCode>EUR</currencyCode>
<startDate>2018-12-06T00:00:00.000</startDate>
</EmpPayCompRecurring>
</EmpJob>
</EmpJob>
我创建了一个 XSLT 转换
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name = "custReturnDate" />
<xsl:template match="/EmpJob">
<xsl:variable name="apos">'</xsl:variable>
<xsl:variable name="payrecur">'1010','6097' </xsl:variable>
<mainroot>
<xsl:for-each select="EmpJob">
<root>
<__metadata>
<uri><xsl:value-of select="concat('EmpCompensation(seqNumber=1L,startDate=datetime',$apos ,$custReturnDate, $apos,',userId=',$apos,userId,$apos,')')"/></uri>
</__metadata>
<customDouble13><xsl:value-of select="sum(EmpPayCompRecurring[$payrecur]/paycompvalue) "/></customDouble13>
</root>
</xsl:for-each>
</mainroot>
</xsl:template>
</xsl:stylesheet>
“payrecur”应该是稍后根据这个参数我应该对节点值求和的参数。
P.S。我可以将参数更改为任何参数,因为我是从代码调用模板。
给定 XSLT 2 或 3,我会将参数声明为字符串或整数序列,然后比较 sum(EmpPayCompRecurring[payComponent = $payrecur]/paycompvalue)
。
所以声明例如<xsl:param name="payrecur" as="xs:string*" select="'1010','6097'"/>
。
最小样式表是
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:param name = "custReturnDate" />
<xsl:param name="payrecur" as="xs:string*" select="'1010','6097'"/>
<xsl:output indent="yes"/>
<xsl:template match="/EmpJob">
<xsl:variable name="apos">'</xsl:variable>
<mainroot>
<xsl:for-each select="EmpJob">
<root>
<__metadata>
<uri><xsl:value-of select="concat('EmpCompensation(seqNumber=1L,startDate=datetime',$apos ,$custReturnDate, $apos,',userId=',$apos,userId,$apos,')')"/></uri>
</__metadata>
<customDouble13>
<xsl:value-of select="sum(EmpPayCompRecurring[payComponent = $payrecur]/paycompvalue)"/>
</customDouble13>
</root>
</xsl:for-each>
</mainroot>
</xsl:template>
</xsl:stylesheet>
在 https://xsltfiddle.liberty-development.net/bEzkTcM 我得到 <customDouble13>3125.67</customDouble13>
。您可能希望在 xsl:stylesheet
元素上添加 exclude-result-prefixes="#all"
。
大家好,如果有人遇到我在 header 中添加参数以便 XSLT 模板获取它之前提到的这种情况。不幸的是,模板将它作为字符串接收,所以我不得不使用函数 tokenize() 将参数转换为数组,然后我可以使用 Martin Honnen 提供的模板。
此方案适用于 Apache Camel 和 SAP CPI
此致
易卜拉欣