3

たとえば、17.95 ドルではなく「20 ドル未満」と言うことができるように、価格を表現された価格帯に変換したいと考えています。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と compare to を使用します。ここで文字列比較を行っている可能性がありますが、これはあなたが望むものではないようです。の外側も定義する必要があるでしょう。20.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このように定義された 3 つの変数がすぐにスコープ外になり、後でそのような変数を参照すると、「変数未定義」エラーが発生するためです。

使用:

               <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 に答える