在 XSLT 多个模板中进行分割、匹配和更新

  • 本文关键字:分割 更新 XSLT xslt xslt-1.0
  • 更新时间 :
  • 英文 :


棘手且有点混乱的问题陈述: 1.获取桌面的价格划分数量值,并将其更新为订单号phoneIDprice>0的值匹配的元素的价格2.同时仅更新名称desktop2桌面 3的元素。一次只能有一个 注意:桌面可以是多个,但使用不同的手机 ID,桌面2桌面3也是如此

需要在单独的模板中使用它,因为我已经有规则只为desktop2desktop3选择几个字段,我正在使用自定义标签并将值复制,例如价格净价,如输出 xml 所示。

输入

<listoforders>
<Orderitem>
<name>Desktop</name>
<place>NZ</place>
<netprice>120</netprice>
<quantity>2</quantity>
<itemproperties>
<name>phoneid</name>
<value>1234</value>
</itemproperties>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>140</price>
<ordernumber>1234</ordernumber>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>0</price>
<ordernumber>1234</ordernumber>           
<objectid>1</objectid>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>0</price>
<ordernumber>1234</ordernumber>        
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>12345</ordernumber>            
<netprice>130</price>
</Orderitem>
<Orderitem>
<name>Desktop</name>
<place>NZ</place>
<price>120</price>
<quantity>3</quantity>
<associationid>2</associationid>
<itemproperties>
<name>phoneid</name>
<value>12345</value>
</itemproperties>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<price>0</price>
<ordernumber>12345</ordernumber>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<price>0</price>
<ordernumber>12345</ordernumber>
<objectid>1</objectid>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<price>0</price>
<ordernumber>12345</ordernumber>
</Orderitem>
</listoforders>

输出:

<listoforders>
<Orderitem>
<name>Desktop</name>
<place>NZ</place>
<netprice>120</netprice>
<quantity>2</quantity>
<itemproperties>
<name>phoneid</name>
<value>1234</value>
</itemproperties>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>140</price>
<ordernumber>1234</ordernumber>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>60</price>
<ordernumber>1234</ordernumber>           
<objectid>1</objectid>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>60</price>
<ordernumber>1234</ordernumber>        
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>12345</ordernumber>            
<netprice>130</price>
</Orderitem>
<Orderitem>
<name>Desktop</name>
<place>NZ</place>
<netprice>120</netprice>
<quantity>3</quantity>
<associationid>2</associationid>
<itemproperties>
<name>phoneid</name>
<value>12345</value>
</itemproperties>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>40</netprice>
<ordernumber>12345</ordernumber>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<netprice>40</netprice>
<ordernumber>12345</ordernumber>
<objectid>1</objectid>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<price>40</price>
<ordernumber>12345</ordernumber>
</Orderitem>
</listoforders>

我使用了一个键,一个变量来匹配和div规则,但我无法跨模板传递给变量。

XSLT:示例 xslt

<xsl:output encoding="UTF-8" omit-xml-declaration="yes"  
indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="partnumber" 
match="Orderitem[name='Desktop']/itemproperties[name='phoneid'])" 
use="value" />
<xsl:template match="Orderitem[name='Desktop2]">
<NetPrice>
<xsl:value-of select="price"/>
</NetPrice>
<xsl:variable select="quant" match="Orderitem[name='Desktop']/quantity" />
<xsl:variable name="amt" match="Orderitem[name='Desktop']/price" />

</xsl:template>
<xsl:template match="Orderitem[../name='Desktop2'][key('partnumber', 
../ordernumber)]/>
<xsl:if test="price = 0"
<Price>xsl:value-of select="$amot div $quant"</Price>
</xsl:if>
<xsl:if test="price != 0"
<Price> xsl:value-of select="price" </Price>  
</xsl:if>
</xsl:template>
</xsl:stylesheet>

基于更新的示例输入 XML,已修改解决方案以满足 XML 中多次出现的桌面值。

该解决方案不使用<xsl:key>而是使用类似的筛选条件来循环和计算输出中所需的<netprice>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- match elements Desktop2 and Desktop3 -->
<xsl:template match="Orderitem[name='Desktop2'] | Orderitem[name='Desktop3']">
<xsl:copy>  
<!-- copy all elements except price -->
<xsl:apply-templates select="*[not(self::price)]" />
<!-- logic for calculating netprice -->
<xsl:call-template name="calcNetPrice">
<xsl:with-param name="price" select="price" />
<xsl:with-param name="orderNum" select="ordernumber" />
</xsl:call-template>
</xsl:copy>
</xsl:template>
<!-- calculate net price -->
<xsl:template name="calcNetPrice">
<xsl:param name="price" />
<xsl:param name="orderNum" />
<xsl:variable name="matchingSet" select="//Orderitem[name='Desktop']/itemproperties[name='phoneid'][value=$orderNum]" />
<netprice>
<xsl:choose>
<!-- check whether a match is found -->
<xsl:when test="$matchingSet != ''">
<!-- loop only for matching set -->
<xsl:for-each select="$matchingSet">
<xsl:choose>
<xsl:when test="$price = 0">
<xsl:value-of select="../price div ../quantity" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$price" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<!-- else set the netprice to the input price -->
<xsl:otherwise>
<xsl:value-of select="$price" />
</xsl:otherwise>
</xsl:choose>
</netprice>
</xsl:template>
</xsl:stylesheet>

输出

<listoforders>
<Orderitem>
<name>Desktop</name>
<place>NZ</place>
<price>120</price>
<quantity>2</quantity>
<associationid>2</associationid>
<itemproperties>
<name>phoneid</name>
<value>1234</value>
</itemproperties>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>1234</ordernumber>
<netprice>120</netprice>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>1234</ordernumber>
<objectid>1</objectid>
<netprice>60</netprice>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>1234</ordernumber>
<netprice>60</netprice>
</Orderitem>
<Orderitem>
<name>Desktop3</name>
<place>NZ</place>
<ordernumber>1234</ordernumber>
<netprice>130</netprice>
</Orderitem>
<Orderitem>
<name>Desktop</name>
<place>NZ</place>
<price>120</price>
<quantity>3</quantity>
<associationid>2</associationid>
<itemproperties>
<name>phoneid</name>
<value>123456</value>
</itemproperties>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>123456</ordernumber>
<netprice>40</netprice>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>123456</ordernumber>
<objectid>1</objectid>
<netprice>40</netprice>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>123456</ordernumber>
<netprice>40</netprice>
</Orderitem>
<Orderitem>
<name>Desktop2</name>
<place>NZ</place>
<ordernumber>123456</ordernumber>
<netprice>130</netprice>
</Orderitem>
</listoforders>

编辑:解决方案修改为处理桌面2桌面3值具有ordernumber与任何桌面itemproperties[name='phoneid']/value不匹配。

相关内容

  • 没有找到相关文章

最新更新