在XSLT多个模板

时间:2017-09-15 12:34:08

标签: xslt xslt-1.0

棘手且有点令人困惑的问题陈述: 1.确定价格划分数量桌面并将其更新为价格元素所在的位置 ordernumber 匹配 phoneid price 的值> 0 2.也只更新 name desktop2 desktop3 的元素。一次只有一个 注意:桌面可以是多个,但使用不同的手机ID, desktop2 desktop3

也是如此

在单独的模板中需要它,因为我已经有规则只选择 desktop2 desktop3 的几个字段,其中我使用自定义标签并复制值,例如价格 netprice ,如输出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>

1 个答案:

答案 0 :(得分:0)

根据更新的示例输入XML,修改了解决方案以满足XML中多次出现的 Desktop 值。

解决方案不使用<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>

修改:修改了解决方案以处理 Desktop2 Desktop3 ordernumber与任何桌面不匹配的值 itemproperties[name='phoneid']/value