首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用标识转换的XSLT1.0与多个模板无法匹配

使用标识转换的XSLT1.0与多个模板无法匹配
EN

Stack Overflow用户
提问于 2020-12-24 15:32:28
回答 1查看 98关注 0票数 1

哇,自从15年前编程入门之后,我就没有问过这么多问题了,因为XSLT的东西让我很不爽!我一直在学习如何使用身份转换,获取字符串并根据分隔符将其分解为节点。但当我把它和我的其他逻辑结合起来的时候,它就会破裂。我想这与我对元素本身的选择有关。我为自己感到骄傲,因为我找到了一个身份转换的基本例子。我一直在工作和学习的各个阶段,最初从这里开始。XSLT 1.0 - Output Count of Value Uniqueness in a Node with Vanilla XSLT 1.0 or PHP XSLT,我认为选择器和操作顺序将是接下来我需要考虑的事情。

任何帮助都将不胜感激。:)

源数据编辑的不使用,因为我最初提供了有缺陷的数据,并在下面添加了更新的数据。

代码语言:javascript
复制
<root>
  <part>
    <partNumber>33020780</partNumber>
    <punctuatedPartNumber>3302-0780</punctuatedPartNumber>
    <partStatusDescription>STANDARD</partStatusDescription>
    <partDescription>Women's Hooligan Glove - Black - 2XL</partDescription>
    <unitOfMeasure>Pair</unitOfMeasure>
    <brandName>ICON</brandName>
    <supplierNumber></supplierNumber>
    <specialInstructions/>
    <baseDealerPrice>15.0000</baseDealerPrice>
    <yourDealerPrice>15.0000</yourDealerPrice>
    <baseRetailPrice>30.0000</baseRetailPrice>
    <originalRetailPrice>30.0000</originalRetailPrice>
    <partImage>http://asset.lemansnet.com/z/L0UvOS8zL0U5M0JDMzM4LUNEMDAtNDhGNi1CMjQzLTJCM0U3ODBCQzU0NCwvRC85LzEvRDkxQUQ1ODQtQjU0Ny00MTYzLUIxNDgtMzk3MTMwOEU2N0U5</partImage>
    <productId>1967508881</productId>
    <productName>Women's Hooligan™ Gloves</productName>
    <productImage>http://asset.lemansnet.com/z/bWVkaWEvYzY1Y2JiYzctYTk4OC00OTY0LWJlYTEtNzYzZDFiYmQ4NzBlLC9FLzkvMy9FOTNCQzMzOC1DRDAwLTQ4RjYtQjI0My0yQjNFNzgwQkM1NDQsejJodWhwWVN1cGc=</productImage>
    <bullet1>Durable nylon/synthetic suede construction</bullet1>
    <bullet2>AxSuede Laredo palm</bullet2>
    <bullet3>Thermoplastic knuckle armor for durable, flexible protection</bullet3>
    <bullet4>Ram air finger inserts help keep hands cool</bullet4>
    <bullet5>Snap secures gloves together when not in use</bullet5>
    <bullet6/>
    <bullet7/>
    <bullet8/>
    <bullet9/>
    <bullet10/>
    <bullet11/>
    <bullet12/>
    <bullet13/>
    <bullet14/>
    <bullet15/>
    <bullet16/>
    <bullet17/>
    <bullet18/>
    <bullet19/>
    <bullet20/>
    <bullet21/>
    <bullet22/>
    <bullet23/>
    <bullet24/>
  </part>
  <part>
    <partNumber>33020783</partNumber>
    <punctuatedPartNumber>3302-0783</punctuatedPartNumber>
    <partStatusDescription>STANDARD</partStatusDescription>
    <partDescription>Women's Hooligan Glove - Pink - Medium</partDescription>
    <unitOfMeasure>Pair</unitOfMeasure>
    <brandName>ICON</brandName>
    <supplierNumber></supplierNumber>
    <specialInstructions/>
    <baseDealerPrice>15.0000</baseDealerPrice>
    <yourDealerPrice>15.0000</yourDealerPrice>
    <baseRetailPrice>30.0000</baseRetailPrice>
    <originalRetailPrice>30.0000</originalRetailPrice>
    <partImage>http://asset.lemansnet.com/z/L0UvRi8zL0VGMzE4NzU3LTZCNDAtNDUyNi1CMjY3LTRBMzYzNTI2RkU1RSwvMC9EL0MvMERDQ0NCQTktMEVEOC00QzI0LThBRDYtOTRDNzVCQUU5NUVC</partImage>
    <productId>1967508881</productId>
    <productName>Women's Hooligan™ Gloves</productName>
    <productImage>http://asset.lemansnet.com/z/bWVkaWEvYzY1Y2JiYzctYTk4OC00OTY0LWJlYTEtNzYzZDFiYmQ4NzBlLC9FLzkvMy9FOTNCQzMzOC1DRDAwLTQ4RjYtQjI0My0yQjNFNzgwQkM1NDQsejJodWhwWVN1cGc=</productImage>
    <bullet1>Durable nylon/synthetic suede construction</bullet1>
    <bullet2>AxSuede Laredo palm</bullet2>
    <bullet3>Thermoplastic knuckle armor for durable, flexible protection</bullet3>
    <bullet4>Ram air finger inserts help keep hands cool</bullet4>
    <bullet5>Snap secures gloves together when not in use</bullet5>
    <bullet6/>
    <bullet7/>
    <bullet8/>
    <bullet9/>
    <bullet10/>
    <bullet11/>
    <bullet12/>
    <bullet13/>
    <bullet14/>
    <bullet15/>
    <bullet16/>
    <bullet17/>
    <bullet18/>
    <bullet19/>
    <bullet20/>
    <bullet21/>
    <bullet22/>
    <bullet23/>
    <bullet24/>
  </part>
</root>

工作身份转换

代码语言:javascript
复制
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="partDescription" name="tokenize">
        <xsl:param name="text" select="."/>
        <xsl:param name="delimiter" select="' - '"/>
        <xsl:param name="keys" select="'short_name,color,size'"/>
        <xsl:element name="{substring-before($keys, ',')}">
            <xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)" />
        </xsl:element>
        <xsl:if test="contains($text, $delimiter)">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                <xsl:with-param name="keys" select="substring-after($keys, ',')"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

失败的完整示例,当我说破坏的结果仅仅是<items/>

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:key name="part-by-product" match="part" use="productId" />
    <xsl:strip-space elements="*"/>

    <!-- Transform Data  -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Parse Description as Keys  -->
    <xsl:template match="partDescription" name="tokenize">
        <xsl:param name="text" select="."/>
        <xsl:param name="delimiter" select="' - '"/>
        <xsl:param name="keys" select="'short_name,color,size'"/>
        <xsl:element name="{substring-before($keys, ',')}">
            <xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)" />
        </xsl:element>
        <xsl:if test="contains($text, $delimiter)">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                <xsl:with-param name="keys" select="substring-after($keys, ',')"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template match="/root">
        <xsl:element name="items">
            <xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
                <xsl:element name="item">
                    
                    <!-- finish -->
                    <xsl:choose>
                        <xsl:when test="contains(partDescription, 'Black')">
                            <finish>Black</finish>
                        </xsl:when>
                        <xsl:when test="contains(partDescription, 'Flat Back')">
                            <finish>Flat Back</finish>
                        </xsl:when>
                        <xsl:when test="contains(partDescription, 'Chrome')">
                            <finish>Chrome</finish>
                        </xsl:when>
                    </xsl:choose>
                    
                    <!-- size -->
                    <xsl:call-template name="find-size">
                        <xsl:with-param name="text" select="partDescription"/>
                    </xsl:call-template>

                    <!-- Check if a Product should be configurable -->
                    <xsl:copy-of select="partNumber | productId"/>
                    <!-- check productId uniqueness -->
                    <xsl:if test="count(key('part-by-product', productId)) > 1">
                        <configurableId>
                            <xsl:value-of select="productId"/>
                        </configurableId>
                    </xsl:if>

                    <xsl:element name="name">
                         <xsl:value-of select="concat(brandName,' ',productName)"/>
                    </xsl:element>
                    <punctuatedPartNumber>
                        <xsl:value-of select="punctuatedPartNumber"/>
                    </punctuatedPartNumber>
                     <xsl:element name="is_in_stock">
                        <xsl:if test="partStatusDescription = 'STANDARD'">
                                <xsl:value-of select="1"/>
                        </xsl:if>
                        <xsl:if test="partStatusDescription != 'STANDARD'">
                            <xsl:value-of select="0"/>
                        </xsl:if>    
                    </xsl:element>
                    <partDescription>
                        <xsl:value-of select="partDescription"/>
                    </partDescription>
                    <unitOfMeasure>
                        <xsl:value-of select="unitOfMeasure"/>
                    </unitOfMeasure>
                    <brandName>
                        <xsl:value-of select="brandName"/>
                    </brandName>
                    <supplierNumber>
                        <xsl:value-of select="supplierNumber"/>
                    </supplierNumber>
                    <specialInstructions>
                        <xsl:value-of select="specialInstructions"/>
                    </specialInstructions>
                    <xsl:element name="price">
                        <xsl:value-of select="(originalRetailPrice * 100) div 100"/>
                    </xsl:element>
                    <xsl:element name="special_price">
                        <xsl:if test="baseRetailPrice  &lt; originalRetailPrice">
                            <xsl:value-of select="baseRetailPrice"/>
                        </xsl:if>
                    </xsl:element>
                    <partImage>
                        <xsl:value-of select="partImage"/>
                    </partImage>
                    <productName>
                        <xsl:value-of select="productName"/>
                    </productName>
                    <productImage>
                        <xsl:value-of select="productImage"/>
                    </productImage>
                    <bullet1>
                        <xsl:value-of select="bullet1"/>
                    </bullet1>
                    <bullet2>
                        <xsl:value-of select="bullet2"/>
                    </bullet2>
                    <bullet3>
                        <xsl:value-of select="bullet3"/>
                    </bullet3>
                    <bullet4>
                        <xsl:value-of select="bullet4"/>
                    </bullet4>

                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

    <xsl:template name="find-size">
        <xsl:param name="text"/>
        <xsl:param name="delimiter" select="' '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:choose>
            <xsl:when test="starts-with(translate($token, '123456789', '000000000'), '0')">
                <size>
                    <xsl:value-of select="$token"/>
                </size>
            </xsl:when>
            <xsl:when test="contains($text, $delimiter)">
                <!-- recursive call -->
                <xsl:call-template name="find-size">
                    <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

*

更新数据样本

我提供了示例数据,这将永远不会发生意外事故,这也解释了为什么我只接收到原始节点元素。然而,即使在修复了我愚蠢的错误之后,我的代码组合也不起作用。

代码语言:javascript
复制
<root>
  <part>
    <partNumber>33020576</partNumber>
    <punctuatedPartNumber>3302-0576</punctuatedPartNumber>
    <partStatusDescription>STANDARD</partStatusDescription>
    <partDescription>Stella SP-8 V2 Gloves - Black/White/Pink - Large</partDescription>
    <unitOfMeasure>Pair</unitOfMeasure>
    <brandName>ALPINESTARS (ROAD)</brandName>
    <supplierNumber>3518317-1239-L</supplierNumber>
    <specialInstructions/>
    <baseDealerPrice>68.0000</baseDealerPrice>
    <yourDealerPrice>68.0000</yourDealerPrice>
    <baseRetailPrice>99.9500</baseRetailPrice>
    <originalRetailPrice>99.9500</originalRetailPrice>
    <partImage>http://asset.lemansnet.com/z/L0EvRC84L0FEODhFNTI1LTBGRjItNEY3NC1CRTBDLTg4RkNBNjdCREVFQw==</partImage>
    <productId>0975297251</productId>
    <productName>Stella SP-8 V2 Leather Gloves</productName>
    <productImage>http://asset.lemansnet.com/z/LzcvMC9CLzcwQkJGNjJGLURDMDUtNDY0RC1BQ0YzLTQxN0FEQ0E3RDgwNCxtZWRpYS82OGFiOGQ5YS0wMDhiLTQ1NDctOGExMC1jZTBhNGU2MmUyNzQsbWVkaWEvYzZlODQwNDEtZTViMy00YTExLWJjYjUtODJiMDQ5NmI4OGZl</productImage>
    <bullet1>Tall, relaxed sport glove</bullet1>
    <bullet2>Premium, full-grain leather construction is durable and offers excellent abrasion resistance.</bullet2>
    <bullet3>An innovative microfiber and Polyurethane coated (PU) grip insert strategically position on palm and thumb for excellent levels of grip control and durability.</bullet3>
    <bullet4>Chassis is profiled for female specific fit and Alpinestars’ exclusive ergonomic stretch insert between palm and thumb offers improved range of hand movement and greater sensitivity while operating the bike controls.</bullet4>
    <bullet5>Incorporates premium quality suede palm and landing reinforcement for grip, control and durability.</bullet5>
    <bullet6>Alpinestars’ patented third and fourth finger bridge prevents seam failure and finger separation in the event of a slide.</bullet6>
    <bullet7>Excellent repeated impact performance offered by an advanced over-molded polymer knuckle protector.</bullet7>
    <bullet8>Ethylene vinyl acetate (EVA) foam padding reinforcements on thumb, wrist, back of hand, finger and palm for comfort and abrasion performance.</bullet8>
    <bullet9>Perforated leather cuff, plus perforated finger sidewalls for superb breathability.</bullet9>
    <bullet10>Velcro cuff closure, plus elasticized wrist design for secure, personalized fit.</bullet10>
    <bullet11>Pre-shaped fingers and external seams for comfort and exceptional feel.</bullet11>
    <bullet12>Printed and embroidered logos on wrist and fingers, plus heat stamped logo on knuckle.</bullet12>
    <bullet13>Touchscreen compatible fingertip on index finger for use with touch screen devices.</bullet13>
    <bullet14/>
    <bullet15/>
    <bullet16/>
    <bullet17/>
    <bullet18/>
    <bullet19/>
    <bullet20/>
    <bullet21/>
    <bullet22/>
    <bullet23/>
    <bullet24/>
  </part>
  <part>
    <partNumber>33020577</partNumber>
    <punctuatedPartNumber>3302-0577</punctuatedPartNumber>
    <partStatusDescription>STANDARD</partStatusDescription>
    <partDescription>Stella SP-8 V2 Gloves - Black/White/Pink - XL</partDescription>
    <unitOfMeasure>Pair</unitOfMeasure>
    <brandName>ALPINESTARS (ROAD)</brandName>
    <supplierNumber>3518317-1239-XL</supplierNumber>
    <specialInstructions/>
    <baseDealerPrice>68.0000</baseDealerPrice>
    <yourDealerPrice>68.0000</yourDealerPrice>
    <baseRetailPrice>99.9500</baseRetailPrice>
    <originalRetailPrice>99.9500</originalRetailPrice>
    <partImage>http://asset.lemansnet.com/z/L0EvRC84L0FEODhFNTI1LTBGRjItNEY3NC1CRTBDLTg4RkNBNjdCREVFQw==</partImage>
    <productId>0975297251</productId>
    <productName>Stella SP-8 V2 Leather Gloves</productName>
    <productImage>http://asset.lemansnet.com/z/LzcvMC9CLzcwQkJGNjJGLURDMDUtNDY0RC1BQ0YzLTQxN0FEQ0E3RDgwNCxtZWRpYS82OGFiOGQ5YS0wMDhiLTQ1NDctOGExMC1jZTBhNGU2MmUyNzQsbWVkaWEvYzZlODQwNDEtZTViMy00YTExLWJjYjUtODJiMDQ5NmI4OGZl</productImage>
    <bullet1>Tall, relaxed sport glove</bullet1>
    <bullet2>Premium, full-grain leather construction is durable and offers excellent abrasion resistance.</bullet2>
    <bullet3>An innovative microfiber and Polyurethane coated (PU) grip insert strategically position on palm and thumb for excellent levels of grip control and durability.</bullet3>
    <bullet4>Chassis is profiled for female specific fit and Alpinestars’ exclusive ergonomic stretch insert between palm and thumb offers improved range of hand movement and greater sensitivity while operating the bike controls.</bullet4>
    <bullet5>Incorporates premium quality suede palm and landing reinforcement for grip, control and durability.</bullet5>
    <bullet6>Alpinestars’ patented third and fourth finger bridge prevents seam failure and finger separation in the event of a slide.</bullet6>
    <bullet7>Excellent repeated impact performance offered by an advanced over-molded polymer knuckle protector.</bullet7>
    <bullet8>Ethylene vinyl acetate (EVA) foam padding reinforcements on thumb, wrist, back of hand, finger and palm for comfort and abrasion performance.</bullet8>
    <bullet9>Perforated leather cuff, plus perforated finger sidewalls for superb breathability.</bullet9>
    <bullet10>Velcro cuff closure, plus elasticized wrist design for secure, personalized fit.</bullet10>
    <bullet11>Pre-shaped fingers and external seams for comfort and exceptional feel.</bullet11>
    <bullet12>Printed and embroidered logos on wrist and fingers, plus heat stamped logo on knuckle.</bullet12>
    <bullet13>Touchscreen compatible fingertip on index finger for use with touch screen devices.</bullet13>
    <bullet14/>
    <bullet15/>
    <bullet16/>
    <bullet17/>
    <bullet18/>
    <bullet19/>
    <bullet20/>
    <bullet21/>
    <bullet22/>
    <bullet23/>
    <bullet24/>
  </part>
</root>
EN

回答 1

Stack Overflow用户

发布于 2020-12-25 00:16:43

嗯,经过一个重大的错误,过滤我的数据,并使用样本数据,本来可以过滤掉。然后,学习,这是一个关键的部分,当您使用“模板匹配”时,您必须谨慎和深思熟虑,通过我最初使用以下内容

代码语言:javascript
复制
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

因此,我最终要做的是转换,从尝试匹配模板级别上的所有内容,而是创建一个基本函数,该函数以一个节点作为参数并返回标记化的结果。虽然,我肯定知道有更有效的方法来完成我的目标,我的XSLT代码看起来像是一个孩子写的,希望有人能从我的挣扎中吸取教训。

为了便于阅读,缩短了版本。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:key name="part-by-product" match="part" use="productId" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/root">
        <xsl:element name="items">
            <xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
                <xsl:element name="item">
                    
                    <!-- Call the custom template, and pass the node partDescription to the template -->
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="text" select="partDescription"/>
                    </xsl:call-template>

                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

    <!-- A template that accepts a node with text passesd as a select  -->
    <xsl:template name="tokenize">
        <xsl:param name="text"/>
        <xsl:param name="delimiter" select="' - '"/>
        <xsl:param name="keys" select="'name,color,size,configurable1,configurable2'"/>
        <xsl:element name="{substring-before($keys, ',')}">
            <xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)" />
        </xsl:element>
        <xsl:if test="contains($text, $delimiter)">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                <xsl:with-param name="keys" select="substring-after($keys, ',')"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

基于已完成的示例,我将在此之后共享我的预期结果

代码语言:javascript
复制
<items>
    <item>
        <!-- This was the main goal this task, and what the above sample completes  -->
        <name>SP-8 V2 Gloves</name>
        <color>Black/White/Yellow</color>
        <size>3XL</size>
        <!-- The rest of the result that can be viewed by running the complete XSTL template -->
        <finish>Black</finish>
        <size>3XL</size>
        <partNumber>33012993</partNumber>
        <productId>0067377912</productId>
        <name>ALPINESTARS (ROAD) SP-8 V2 Gloves</name>
        <punctuatedPartNumber>3301-2993</punctuatedPartNumber>
        <is_in_stock>1</is_in_stock>
        <partDescription>SP-8 V2 Gloves - Black/White/Yellow - 3XL</partDescription>
        <unitOfMeasure>Pair</unitOfMeasure>
        <brandName>ALPINESTARS (ROAD)</brandName>
        <supplierNumber>3558317-125-3X</supplierNumber>
        <specialInstructions/>
        <price>99.95</price>
        <special_price/>
        <partImage>http://asset.lemansnet.com/z/LzkvNy9FLzk3RUYyQTg3LTM4QTItNDZEQy1CNzE2LThGMjNEQjQxNzgzOA==</partImage>
        <productName>SP-8 V2 Gloves</productName>
        <productImage>http://asset.lemansnet.com/z/L0UvRS9FL0VFRUEwQzRCLUZCRDktNDUzQS1CMTg3LUVCMTY2OUM5N0ZDQywvRi8yL0MvRjJDQjRERTMtQUM2MC00M0ZCLUFERDktMzE0NTA4QTIxQjcyLC8zLzAvNy8zMDdGRTEzMy0zMUFFLTQzQ0ItOTM3MC00NzY1OEI3RUM4MUEsbWVkaWEvNjhhYjhkOWEtMDA4Yi00NTQ3LThhMTAtY2UwYTRlNjJlMjc0LG1lZGlhL2M2ZTg0MDQxLWU1YjMtNGExMS1iY2I1LTgyYjA0OTZiODhmZQ==</productImage>
        <bullet1>Premium, full-grain leather construction is durable and offers excellent abrasion resistance</bullet1>
        <bullet2>An innovative microfiber and Polyurethane (PU) grip insert strategically position on palm and thumb</bullet2>
        <bullet3>Alpinestars’ exclusive ergonomic stretch insert between palm and thumb</bullet3>
        <bullet4>Premium quality suede palm and landing reinforcement.</bullet4>
    </item>
</items>

基于我最初的示例完成的版本

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:key name="part-by-product" match="part" use="productId" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/root">
        <xsl:element name="items">
            <xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
                <xsl:element name="item">
                    
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="text" select="partDescription"/>
                    </xsl:call-template>

                    <!-- finish -->
                    <xsl:choose>
                        <xsl:when test="contains(partDescription, 'Black')">
                            <finish>Black</finish>
                        </xsl:when>
                        <xsl:when test="contains(partDescription, 'Flat Back')">
                            <finish>Flat Back</finish>
                        </xsl:when>
                        <xsl:when test="contains(partDescription, 'Chrome')">
                            <finish>Chrome</finish>
                        </xsl:when>
                    </xsl:choose>
                    
                    <!-- size -->
                    <xsl:call-template name="find-size">
                        <xsl:with-param name="text" select="partDescription"/>
                    </xsl:call-template>

                    <!-- Check if a Product should be configurable -->
                    <xsl:copy-of select="partNumber | productId"/>
                    <!-- check productId uniqueness -->
                    <xsl:if test="count(key('part-by-product', productId)) > 1">
                        <configurableId>
                            <xsl:value-of select="productId"/>
                        </configurableId>
                    </xsl:if>

                    <xsl:element name="name">
                         <xsl:value-of select="concat(brandName,' ',productName)"/>
                    </xsl:element>
                    <punctuatedPartNumber>
                        <xsl:value-of select="punctuatedPartNumber"/>
                    </punctuatedPartNumber>
                     <xsl:element name="is_in_stock">
                        <xsl:if test="partStatusDescription = 'STANDARD'">
                                <xsl:value-of select="1"/>
                        </xsl:if>
                        <xsl:if test="partStatusDescription != 'STANDARD'">
                            <xsl:value-of select="0"/>
                        </xsl:if>    
                    </xsl:element>
                    <partDescription>
                        <xsl:value-of select="partDescription"/>
                    </partDescription>
                    <unitOfMeasure>
                        <xsl:value-of select="unitOfMeasure"/>
                    </unitOfMeasure>
                    <brandName>
                        <xsl:value-of select="brandName"/>
                    </brandName>
                    <supplierNumber>
                        <xsl:value-of select="supplierNumber"/>
                    </supplierNumber>
                    <specialInstructions>
                        <xsl:value-of select="specialInstructions"/>
                    </specialInstructions>
                    <xsl:element name="price">
                        <xsl:value-of select="(originalRetailPrice * 100) div 100"/>
                    </xsl:element>
                    <xsl:element name="special_price">
                        <xsl:if test="baseRetailPrice  &lt; originalRetailPrice">
                            <xsl:value-of select="baseRetailPrice"/>
                        </xsl:if>
                    </xsl:element>
                    <partImage>
                        <xsl:value-of select="partImage"/>
                    </partImage>
                    <productName>
                        <xsl:value-of select="productName"/>
                    </productName>
                    <productImage>
                        <xsl:value-of select="productImage"/>
                    </productImage>
                    <bullet1>
                        <xsl:value-of select="bullet1"/>
                    </bullet1>
                    <bullet2>
                        <xsl:value-of select="bullet2"/>
                    </bullet2>
                    <bullet3>
                        <xsl:value-of select="bullet3"/>
                    </bullet3>
                    <bullet4>
                        <xsl:value-of select="bullet4"/>
                    </bullet4>

                </xsl:element>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

    <!-- Parse Description as Keys  -->
    <xsl:template name="tokenize">
        <xsl:param name="text"/>
        <xsl:param name="delimiter" select="' - '"/>
        <xsl:param name="keys" select="'name,color,size,configurable1,configurable2'"/>
        <xsl:element name="{substring-before($keys, ',')}">
            <xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)" />
        </xsl:element>
        <xsl:if test="contains($text, $delimiter)">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                <xsl:with-param name="keys" select="substring-after($keys, ',')"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <!-- Size Template Engine -->
    <xsl:template name="find-size">
        <xsl:param name="text"/>
        <xsl:param name="delimiter" select="' '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:choose>
            <xsl:when test="starts-with(translate($token, '123456789', '000000000'), '0')">
                <size>
                    <xsl:value-of select="$token"/>
                </size>
            </xsl:when>
            <xsl:when test="contains($text, $delimiter)">
                <!-- recursive call -->
                <xsl:call-template name="find-size">
                    <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65440333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档