3

想将价格转换为明确的价格范围,例如,我可以说“低于 20 美元”而不是 17.95 美元。使用 xml:choose 对我有用,但是当我尝试将结果放入类属性时,出现错误:

XSLT 转换期间出错:发生未知错误 ()

我一直在阅读 xsl:variable 但在这种情况下似乎找不到设置变量的正确方法。

XML

    <?xml version="1.0" encoding="UTF-8"?>
    <productSearchResponse>
        <status>SUCCESS</status>
        <products found="20">
            <product>
                <itemNumber>50575</itemNumber>
                <itemName>Example 1</itemName>
                <price>$ 17.95</price>
            </product>

            ...

            <product>
                <itemNumber>81588</itemNumber>
                <itemName>Example 2</itemName>
                <price>$ 25.95</price>
            </product>
        </products>
    </productSearchResponse>

样式表

<?xml version="1.0" encoding="UTF-8"?>          
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <html>
        <head>
        </head>
        <body>
            <xsl:for-each select="/essentials/webservice">
                <xsl:for-each select="document(@filename)/productSearchResponse/products/product">                  
                    <xsl:variable name="producingCountry" select="producingCountry"/>
                    <xsl:variable name="price" select="price"/>
                    <xsl:variable name="priceNoSymbol" select="translate($price,'&#36;', '')"/> 

                    <!-- Assign pricing bands -->                       
                        <xsl:choose>                            
                            <xsl:when test="$priceNoSymbol &lt; '20'">  
                                <xsl:variable name="priceBand" select="'under20'" />
                            </xsl:when>                             
                            <xsl:when test="$priceNoSymbol &gt; '39.99'">   
                                <xsl:variable name="priceBand" select="'20to40'" />
                            </xsl:when>                             
                            <xsl:otherwise>
                                <xsl:variable name="priceBand" select="'over40'" /> 
                            </xsl:otherwise>                                
                        </xsl:choose>

                    <!-- Make the product div -->
                        <div class="product {$producingCountry} {$price} {$priceNoSymbol}     ">    
                        <!--           How do I insert priceBand into the class attribute? ^ -->    
                            <xsl:apply-templates select="itemName"/><br/>                           
                            <xsl:apply-templates select="producingCountry"/><br/>                       
                            <xsl:apply-templates select="price"/><br/><br/>                                                 
                        </div>

                </xsl:for-each>
            </xsl:for-each>
        </body>         
    </html>
</xsl:template>
</xsl:stylesheet>

生成的 html

    <html>
    <head>
        <body>

            <div class="product France $ 17.95 17.95 ">
                Example 1
                <br>
                France
                <br>
                $ 17.95
                <br>
                <br>
            </div>

            ...

            <div class="product Portugal $ 25.95 25.95 ">
                Example 2
                <br>
                Portugal
                <br>
                $ 25.95
                <br>
                <br>
            </div>

        </body>
    </html>

所需的 html

    <html>
    <head>
        <body>

            <div class="product France $ 17.95 17.95 under20 ">
                Example 1
                <br>
                France
                <br>
                $ 17.95
                <br>
                <br>
            </div>

            ...

            <div class="product Portugal $ 25.95 25.95 20to40">
                Example 2
                <br>
                Portugal
                <br>
                $ 25.95
                <br>
                <br>
            </div>

        </body>
    </html>
4

3 回答 3

5

我没有详细研究整个问题,但你应该替换

<xsl:choose>                            
  <xsl:when test="$priceNoSymbol &lt; '20'">  
    <xsl:variable name="priceBand" select="'under20'" />
  </xsl:when>                             
  <xsl:when test="$priceNoSymbol &gt; '39.99'">   
    <xsl:variable name="priceBand" select="'20to40'" />
  </xsl:when>                             
  <xsl:otherwise>
    <xsl:variable name="priceBand" select="'over40'" /> 
  </xsl:otherwise>                                
</xsl:choose>

<xsl:variable name="priceband">
  <xsl:choose>                            
    <xsl:when test="$priceNoSymbol &lt; 20">under20</xsl:when>                             
    <xsl:when test="$priceNoSymbol &lt; 40">20to40</xsl:when>                             
    <xsl:otherwise>over40</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:variable name="priceNoSymbol" select="translate($price,'&#36;', '')"/> 

<xsl:variable name="priceNoSymbol" select="number(translate($price,'&#36;', ''))"/> 

当您在这样的xsl:when元素内定义变量时,它几乎立即超出范围,并且在xsl:choose.

于 2012-12-19T15:20:57.697 回答
1

使用number()on$priceNoSymbol和比较 to20.00而不是'20.00'. 您可能在这里进行字符串比较,这似乎不是您想要的。您可能还需要$priceBandxsl:when.

于 2012-12-19T15:21:32.733 回答
1
                    <xsl:choose>                            
                        <xsl:when test="$priceNoSymbol &lt; '20'">  
                            <xsl:variable name="priceBand" select="'under20'" />
                        </xsl:when>                             
                        <xsl:when test="$priceNoSymbol &gt; '39.99'">   
                            <xsl:variable name="priceBand" select="'20to40'" />
                        </xsl:when>                             
                        <xsl:otherwise>
                            <xsl:variable name="priceBand" select="'over40'" /> 
                        </xsl:otherwise>                                
                    </xsl:choose>

原因是这样定义的三个变量priceBand,立即超出范围,当你稍后引用这样的变量时,结果是“变量未定义”错误。

使用

               <xsl:variable name="priceBand">                       
                   <xsl:choose>                            
                        <xsl:when test=
                       "$priceNoSymbol &lt; 20">under20</xsl:when>                             
                        <xsl:when test=
                        "$priceNoSymbol > 39.99">20to40</xsl:when>                             
                        <xsl:otherwise>over40</xsl:otherwise>                                
                    </xsl:choose>
               </xsl:variable>
于 2012-12-19T15:27:06.067 回答