XSLT 1.0 - 检查一组允许的值,如果找到 return 找到哪个值
XSLT 1.0 - Check for an allowed set of values, and if found return which value was found
问题是,我的数据以 XML 格式存在,但是,关键数据片段未在单独的节点中提供。例如物品的尺寸、颜色、合身性。虽然我通常会提供我尝试过的内容,但我什至不知道从哪里开始。我发现了很多关于如何检查是否找到一个值的示例,但没有找到如何获取匹配值,然后在元素中 return 它的示例。我只能使用 xslt 1.0
这是一个关键部分,我需要帮助,但在此 post 的下方,我包括完整的示例数据和我当前的 XSL 模板
<partDescription>Black 10" Handlebar</partDescription>
<!-- Logic to Figure out Attribute Data from Names, Descriptions -->
<!-- If <partDescription> contains Black, Flat Back, Chrome element finish shoud return the value that is found -->
<xsl:if test="finishIsFound">
<xsl:element name="finish">
<xsl:value-of select="$finishFoundFromDescription"/>
</xsl:element>
</xsl:if>
<!-- If <partDescription> contains a number, grab that number and set as value e.g. 8,14,16-->
<xsl:if test="sizeIsFound">
<xsl:element name="size">
<xsl:value-of select="$sizeFoundFromDescription"/>
</xsl:element>
</xsl:if>
这是当前数据的示例
<part>
<partNumber>06013464</partNumber>
<punctuatedPartNumber>0601-3464</punctuatedPartNumber>
<partStatusDescription>DISCONTINUED</partStatusDescription>
<partDescription>Flat Black 8" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-18M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzgvQS80LzhBNDg5QjE2LUNEQkMtNDAzMC04NkE0LTlEQjE1MDdGRDYzMQ==</partImage>
<productId>1136194258</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012660</partNumber>
<punctuatedPartNumber>0601-2660</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 10" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-10B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzkvMC9DLzkwQzQ2MjZGLTQyOEItNDNGRi1CQjYwLTNBREMyOUM1REU4MQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012663</partNumber>
<punctuatedPartNumber>0601-2663</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 14" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-14</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzcvNC81Lzc0NUE3OUM4LTBERDAtNDQ3NS1CMEFBLUVBMDUyRkUzN0Q5OA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012665</partNumber>
<punctuatedPartNumber>0601-2665</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 16" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-16</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzMvOC8xLzM4MUQ3NEEwLUM0NEMtNDQxNS05MjA3LUVENjBBQjBCRDhGMQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012666</partNumber>
<punctuatedPartNumber>0601-2666</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 16" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-16B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzMvNC84LzM0ODNENTlBLTMwRjYtNDkyQi04MkE0LTQ4MEJFM0Q4MDY2Qg==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012667</partNumber>
<punctuatedPartNumber>0601-2667</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 18" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-18</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0YvQS9DL0ZBQzMzMEE4LTJDQTItNEI5NS1CNjk0LURFNjA5RTFGODUyQw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012668</partNumber>
<punctuatedPartNumber>0601-2668</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 18" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-18B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0MvOC8yL0M4MkJENjZDLTc4OTUtNDEwOC04NjVELUFDMEZGODEzOENBQw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012669</partNumber>
<punctuatedPartNumber>0601-2669</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 20" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-20</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0IvNS9FL0I1RTk1MzdGLTRDMjItNEY1RC1BMzRGLTJBQzA5MzdFQ0UwOA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012670</partNumber>
<punctuatedPartNumber>0601-2670</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 20" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-20B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzQvQS8zLzRBMzcyMzJDLTJCMDgtNDk1NS1BNENFLTU5M0EzMTQ5RUNGQQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013460</partNumber>
<punctuatedPartNumber>0601-3460</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 10" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-10M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzIvMS81LzIxNURERkMxLTg5OTctNEJEQS05MUVELUZFNDY4RkQ0QThFMQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013461</partNumber>
<punctuatedPartNumber>0601-3461</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 12" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-12M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzIvQy8yLzJDMkE2NURFLTU4QjYtNEVDOC1BRTVCLTI3M0QyMjY0ODAyNw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013462</partNumber>
<punctuatedPartNumber>0601-3462</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 14" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-14M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0EvNC9GL0E0RjJBMkExLUU1QUYtNDJCOC1CQzE2LUY3MEREMTQxRkMwQw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013463</partNumber>
<punctuatedPartNumber>0601-3463</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 16" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-16M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzgvMC9GLzgwRjVFN0I1LThGMzgtNEMwNS04N0YwLUNDODA3NEU0RTY0MA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012662</partNumber>
<punctuatedPartNumber>0601-2662</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 12" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-12B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0UvRS85L0VFOTJEOTEyLTMyNUYtNEI4Ri04NjgyLTIzNDZGRjJERTVGOA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012661</partNumber>
<punctuatedPartNumber>0601-2661</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 12" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-12</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0QvOC80L0Q4NDJFQTg1LUMwMUItNERENC05REVGLUUzM0ExOUQxN0ZFRA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012659</partNumber>
<punctuatedPartNumber>0601-2659</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 10" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-10</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzQvMy81LzQzNTYwRjE4LTBEMUEtNEU3NS04OUU2LUQ2RURGM0MxMENGQg==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012664</partNumber>
<punctuatedPartNumber>0601-2664</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 14" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-14B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0YvRC9DL0ZEQzgwNDU4LUMyQjctNDk1QS04RjMxLTQzRkNERDg2MTBGRQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06010612</partNumber>
<punctuatedPartNumber>0601-0612</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Beach Bar Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7312-01</supplierNumber>
<specialInstructions/>
<baseDealerPrice>106.5000</baseDealerPrice>
<yourDealerPrice>106.5000</yourDealerPrice>
<baseRetailPrice>157.9500</baseRetailPrice>
<originalRetailPrice>157.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0IvMy8zL0IzM0NGMjQxLTk0NzctNDQ5QS05QzZCLTBFMzQwMDdEQTdDNA==</partImage>
<productId>0635669078</productId>
<productName>Hefty 1-1/4" Handlebar — Beach Bar/Hefty</productName>
</part>
<part>
<partNumber>06011482</partNumber>
<punctuatedPartNumber>0601-1482</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black Beach Bar Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7312-01B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>106.5000</baseDealerPrice>
<yourDealerPrice>106.5000</yourDealerPrice>
<baseRetailPrice>157.9500</baseRetailPrice>
<originalRetailPrice>157.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzMvRS82LzNFNjlFOTdDLTg5NjEtNDU1MC1BRkY3LUExNjU0MTdEQUQ5MQ==</partImage>
<productId>0635669078</productId>
<productName>Hefty 1-1/4" Handlebar — Beach Bar/Hefty</productName>
</part>
</root>
然后,我有了当前的 XSL 模板
<?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"/>
<xsl:template match="root">
<xsl:element name="items">
<xsl:for-each select="part">
<xsl:if test="supplierNumber != '' and partStatusDescription != 'DISCONTINUED' ">
<xsl:element name="item">
<partNumber>
<xsl:value-of select="partNumber"/>
</partNumber>
<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 < originalRetailPrice">
<xsl:value-of select="baseRetailPrice"/>
</xsl:if>
</xsl:element>
<partImage>
<xsl:value-of select="partImage"/>
</partImage>
<productId>
<xsl:value-of select="productId"/>
</productId>
<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>
<!-- Logic to Figure out Attribute Data from Names, Descriptions -->
<!-- If <partDescription> contains Black, Flat Back, Chrome element finish shoud return the value that is found -->
<xsl:if test="finishIsFound">
<xsl:element name="finish">
<xsl:value-of select="$finishFoundFromDescription"/>
</xsl:element>
</xsl:if>
<!-- If <partDescription> contains a number, grab that number and set as value e.g. 8,14,16-->
<xsl:if test="sizeIsFound">
<xsl:element name="size">
<xsl:value-of select="$sizeFoundFromDescription"/>
</xsl:element>
</xsl:if>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这真是一团糟。
一团糟,因为 XSLT 旨在处理 结构化 数据,但您的数据提供者选择不利用 XML 提供的结构。正则表达式可以更好地处理此处所需的处理类型 - 然而,正则表达式支持仅在 XSLT 2.0 中引入。
也就是说,让我们看看可以做什么:
第一个任务:
If <partDescription>
contains Black, Flat Back, Chrome element finish shoud return the value that is found
相对简单:假设可能的完成选项是互斥的,你可以这样做:
<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>
请注意,这还假设一个选项不包含在另一个选项中。如果不是这种情况,请确保在包含选项之前测试包含选项。
现在关于尺码,你说:
If <partDescription>
contains a number, grab that number
从字面上看,你可以这样做:
<xsl:variable name="size" select="translate(partDescription, translate(partDescription, '0123456789', ''), '')" />
<xsl:if test="$size">
<size>
<xsl:value-of select="$size"/>
</size>
</xsl:if>
但是,这有两个缺陷:
- 它删除单位(如果大小不是整数,还删除小数点分隔符);
- 它会将
partDescription
中包含的所有数字连接在一起,即使它们不属于同一数字。
更好的(虽然仍然不完美)解决方案是通过 space 字符标记描述,并寻找以数字开头的标记。幸运的是,libxslt
处理器支持EXSLT str:tokenize()
扩展功能,所以你可以这样做:
<xsl:variable name="size" select="str:tokenize(partDescription, ' ')[starts-with(translate(., '123456789', '000000000'), '0')]" />
<xsl:if test="$size">
<size>
<xsl:value-of select="$size"/>
</size>
</xsl:if>
但是如果描述包含多个以数字开头的单词并且尺寸不是其中的第一个单词,这仍然会失败。
下面是两个建议的最小实现:
XSLT 1.0 + EXSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<items>
<xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
<item>
<xsl:copy-of select="partNumber"/>
<!-- 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:variable name="size" select="str:tokenize(partDescription, ' ')[starts-with(translate(., '23456789', '1'), '1')]" />
<xsl:if test="$size">
<size>
<xsl:value-of select="$size"/>
</size>
</xsl:if>
</item>
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>
当应用于您的输入示例时,这将 return:
结果
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<partNumber>06012660</partNumber>
<finish>Black</finish>
<size>10"</size>
</item>
<item>
<partNumber>06012663</partNumber>
<finish>Chrome</finish>
<size>14"</size>
</item>
<item>
<partNumber>06012665</partNumber>
<finish>Chrome</finish>
<size>16"</size>
</item>
<item>
<partNumber>06012666</partNumber>
<finish>Black</finish>
<size>16"</size>
</item>
<item>
<partNumber>06012667</partNumber>
<finish>Chrome</finish>
<size>18"</size>
</item>
<item>
<partNumber>06012668</partNumber>
<finish>Black</finish>
<size>18"</size>
</item>
<item>
<partNumber>06012669</partNumber>
<finish>Chrome</finish>
<size>20"</size>
</item>
<item>
<partNumber>06012670</partNumber>
<finish>Black</finish>
<size>20"</size>
</item>
<item>
<partNumber>06013460</partNumber>
<finish>Black</finish>
<size>10"</size>
</item>
<item>
<partNumber>06013461</partNumber>
<finish>Black</finish>
<size>12"</size>
</item>
<item>
<partNumber>06013462</partNumber>
<finish>Black</finish>
<size>14"</size>
</item>
<item>
<partNumber>06013463</partNumber>
<finish>Black</finish>
<size>16"</size>
</item>
<item>
<partNumber>06012662</partNumber>
<finish>Black</finish>
<size>12"</size>
</item>
<item>
<partNumber>06012661</partNumber>
<finish>Chrome</finish>
<size>12"</size>
</item>
<item>
<partNumber>06012659</partNumber>
<finish>Chrome</finish>
<size>10"</size>
</item>
<item>
<partNumber>06012664</partNumber>
<finish>Black</finish>
<size>14"</size>
</item>
<item>
<partNumber>06010612</partNumber>
</item>
<item>
<partNumber>06011482</partNumber>
<finish>Black</finish>
</item>
</items>
已添加:
这里是纯 XSLT 1.0 中的相同内容,使用递归命名模板而不是扩展函数:
XSLT 1.0
<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:template match="/root">
<items>
<xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
<item>
<xsl:copy-of select="partNumber"/>
<!-- 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>
</item>
</xsl:for-each>
</items>
</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>
问题是,我的数据以 XML 格式存在,但是,关键数据片段未在单独的节点中提供。例如物品的尺寸、颜色、合身性。虽然我通常会提供我尝试过的内容,但我什至不知道从哪里开始。我发现了很多关于如何检查是否找到一个值的示例,但没有找到如何获取匹配值,然后在元素中 return 它的示例。我只能使用 xslt 1.0
这是一个关键部分,我需要帮助,但在此 post 的下方,我包括完整的示例数据和我当前的 XSL 模板
<partDescription>Black 10" Handlebar</partDescription>
<!-- Logic to Figure out Attribute Data from Names, Descriptions -->
<!-- If <partDescription> contains Black, Flat Back, Chrome element finish shoud return the value that is found -->
<xsl:if test="finishIsFound">
<xsl:element name="finish">
<xsl:value-of select="$finishFoundFromDescription"/>
</xsl:element>
</xsl:if>
<!-- If <partDescription> contains a number, grab that number and set as value e.g. 8,14,16-->
<xsl:if test="sizeIsFound">
<xsl:element name="size">
<xsl:value-of select="$sizeFoundFromDescription"/>
</xsl:element>
</xsl:if>
这是当前数据的示例
<part>
<partNumber>06013464</partNumber>
<punctuatedPartNumber>0601-3464</punctuatedPartNumber>
<partStatusDescription>DISCONTINUED</partStatusDescription>
<partDescription>Flat Black 8" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-18M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzgvQS80LzhBNDg5QjE2LUNEQkMtNDAzMC04NkE0LTlEQjE1MDdGRDYzMQ==</partImage>
<productId>1136194258</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012660</partNumber>
<punctuatedPartNumber>0601-2660</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 10" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-10B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzkvMC9DLzkwQzQ2MjZGLTQyOEItNDNGRi1CQjYwLTNBREMyOUM1REU4MQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012663</partNumber>
<punctuatedPartNumber>0601-2663</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 14" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-14</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzcvNC81Lzc0NUE3OUM4LTBERDAtNDQ3NS1CMEFBLUVBMDUyRkUzN0Q5OA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012665</partNumber>
<punctuatedPartNumber>0601-2665</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 16" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-16</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzMvOC8xLzM4MUQ3NEEwLUM0NEMtNDQxNS05MjA3LUVENjBBQjBCRDhGMQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012666</partNumber>
<punctuatedPartNumber>0601-2666</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 16" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-16B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzMvNC84LzM0ODNENTlBLTMwRjYtNDkyQi04MkE0LTQ4MEJFM0Q4MDY2Qg==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012667</partNumber>
<punctuatedPartNumber>0601-2667</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 18" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-18</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0YvQS9DL0ZBQzMzMEE4LTJDQTItNEI5NS1CNjk0LURFNjA5RTFGODUyQw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012668</partNumber>
<punctuatedPartNumber>0601-2668</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 18" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-18B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0MvOC8yL0M4MkJENjZDLTc4OTUtNDEwOC04NjVELUFDMEZGODEzOENBQw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012669</partNumber>
<punctuatedPartNumber>0601-2669</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 20" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-20</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0IvNS9FL0I1RTk1MzdGLTRDMjItNEY1RC1BMzRGLTJBQzA5MzdFQ0UwOA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012670</partNumber>
<punctuatedPartNumber>0601-2670</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 20" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-20B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzQvQS8zLzRBMzcyMzJDLTJCMDgtNDk1NS1BNENFLTU5M0EzMTQ5RUNGQQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013460</partNumber>
<punctuatedPartNumber>0601-3460</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 10" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-10M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzIvMS81LzIxNURERkMxLTg5OTctNEJEQS05MUVELUZFNDY4RkQ0QThFMQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013461</partNumber>
<punctuatedPartNumber>0601-3461</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 12" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-12M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzIvQy8yLzJDMkE2NURFLTU4QjYtNEVDOC1BRTVCLTI3M0QyMjY0ODAyNw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013462</partNumber>
<punctuatedPartNumber>0601-3462</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 14" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-14M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0EvNC9GL0E0RjJBMkExLUU1QUYtNDJCOC1CQzE2LUY3MEREMTQxRkMwQw==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06013463</partNumber>
<punctuatedPartNumber>0601-3463</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Flat Black 16" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-16M</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzgvMC9GLzgwRjVFN0I1LThGMzgtNEMwNS04N0YwLUNDODA3NEU0RTY0MA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012662</partNumber>
<punctuatedPartNumber>0601-2662</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 12" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-12B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0UvRS85L0VFOTJEOTEyLTMyNUYtNEI4Ri04NjgyLTIzNDZGRjJERTVGOA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012661</partNumber>
<punctuatedPartNumber>0601-2661</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 12" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-12</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0QvOC80L0Q4NDJFQTg1LUMwMUItNERENC05REVGLUUzM0ExOUQxN0ZFRA==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012659</partNumber>
<punctuatedPartNumber>0601-2659</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Chrome 10" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-10</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzQvMy81LzQzNTYwRjE4LTBEMUEtNEU3NS04OUU2LUQ2RURGM0MxMENGQg==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06012664</partNumber>
<punctuatedPartNumber>0601-2664</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black 14" Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7321-14B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>185.0000</baseDealerPrice>
<yourDealerPrice>185.0000</yourDealerPrice>
<baseRetailPrice>272.9500</baseRetailPrice>
<originalRetailPrice>272.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0YvRC9DL0ZEQzgwNDU4LUMyQjctNDk1QS04RjMxLTQzRkNERDg2MTBGRQ==</partImage>
<productId>0243176820</productId>
<productName>1-1/4" Touring Ape Hanger Handlebar</productName>
</part>
<part>
<partNumber>06010612</partNumber>
<punctuatedPartNumber>0601-0612</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Beach Bar Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7312-01</supplierNumber>
<specialInstructions/>
<baseDealerPrice>106.5000</baseDealerPrice>
<yourDealerPrice>106.5000</yourDealerPrice>
<baseRetailPrice>157.9500</baseRetailPrice>
<originalRetailPrice>157.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/L0IvMy8zL0IzM0NGMjQxLTk0NzctNDQ5QS05QzZCLTBFMzQwMDdEQTdDNA==</partImage>
<productId>0635669078</productId>
<productName>Hefty 1-1/4" Handlebar — Beach Bar/Hefty</productName>
</part>
<part>
<partNumber>06011482</partNumber>
<punctuatedPartNumber>0601-1482</punctuatedPartNumber>
<partStatusDescription>STANDARD</partStatusDescription>
<partDescription>Black Beach Bar Handlebar</partDescription>
<unitOfMeasure>Each</unitOfMeasure>
<brandName>LA CHOPPERS</brandName>
<supplierNumber>LA-7312-01B</supplierNumber>
<specialInstructions/>
<baseDealerPrice>106.5000</baseDealerPrice>
<yourDealerPrice>106.5000</yourDealerPrice>
<baseRetailPrice>157.9500</baseRetailPrice>
<originalRetailPrice>157.9500</originalRetailPrice>
<partImage>http://asset.lemansnet.com/z/LzMvRS82LzNFNjlFOTdDLTg5NjEtNDU1MC1BRkY3LUExNjU0MTdEQUQ5MQ==</partImage>
<productId>0635669078</productId>
<productName>Hefty 1-1/4" Handlebar — Beach Bar/Hefty</productName>
</part>
</root>
然后,我有了当前的 XSL 模板
<?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"/>
<xsl:template match="root">
<xsl:element name="items">
<xsl:for-each select="part">
<xsl:if test="supplierNumber != '' and partStatusDescription != 'DISCONTINUED' ">
<xsl:element name="item">
<partNumber>
<xsl:value-of select="partNumber"/>
</partNumber>
<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 < originalRetailPrice">
<xsl:value-of select="baseRetailPrice"/>
</xsl:if>
</xsl:element>
<partImage>
<xsl:value-of select="partImage"/>
</partImage>
<productId>
<xsl:value-of select="productId"/>
</productId>
<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>
<!-- Logic to Figure out Attribute Data from Names, Descriptions -->
<!-- If <partDescription> contains Black, Flat Back, Chrome element finish shoud return the value that is found -->
<xsl:if test="finishIsFound">
<xsl:element name="finish">
<xsl:value-of select="$finishFoundFromDescription"/>
</xsl:element>
</xsl:if>
<!-- If <partDescription> contains a number, grab that number and set as value e.g. 8,14,16-->
<xsl:if test="sizeIsFound">
<xsl:element name="size">
<xsl:value-of select="$sizeFoundFromDescription"/>
</xsl:element>
</xsl:if>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这真是一团糟。
一团糟,因为 XSLT 旨在处理 结构化 数据,但您的数据提供者选择不利用 XML 提供的结构。正则表达式可以更好地处理此处所需的处理类型 - 然而,正则表达式支持仅在 XSLT 2.0 中引入。
也就是说,让我们看看可以做什么:
第一个任务:
If
<partDescription>
contains Black, Flat Back, Chrome element finish shoud return the value that is found
相对简单:假设可能的完成选项是互斥的,你可以这样做:
<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>
请注意,这还假设一个选项不包含在另一个选项中。如果不是这种情况,请确保在包含选项之前测试包含选项。
现在关于尺码,你说:
If
<partDescription>
contains a number, grab that number
从字面上看,你可以这样做:
<xsl:variable name="size" select="translate(partDescription, translate(partDescription, '0123456789', ''), '')" />
<xsl:if test="$size">
<size>
<xsl:value-of select="$size"/>
</size>
</xsl:if>
但是,这有两个缺陷:
- 它删除单位(如果大小不是整数,还删除小数点分隔符);
- 它会将
partDescription
中包含的所有数字连接在一起,即使它们不属于同一数字。
更好的(虽然仍然不完美)解决方案是通过 space 字符标记描述,并寻找以数字开头的标记。幸运的是,libxslt
处理器支持EXSLT str:tokenize()
扩展功能,所以你可以这样做:
<xsl:variable name="size" select="str:tokenize(partDescription, ' ')[starts-with(translate(., '123456789', '000000000'), '0')]" />
<xsl:if test="$size">
<size>
<xsl:value-of select="$size"/>
</size>
</xsl:if>
但是如果描述包含多个以数字开头的单词并且尺寸不是其中的第一个单词,这仍然会失败。
下面是两个建议的最小实现:
XSLT 1.0 + EXSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<items>
<xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
<item>
<xsl:copy-of select="partNumber"/>
<!-- 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:variable name="size" select="str:tokenize(partDescription, ' ')[starts-with(translate(., '23456789', '1'), '1')]" />
<xsl:if test="$size">
<size>
<xsl:value-of select="$size"/>
</size>
</xsl:if>
</item>
</xsl:for-each>
</items>
</xsl:template>
</xsl:stylesheet>
当应用于您的输入示例时,这将 return:
结果
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<partNumber>06012660</partNumber>
<finish>Black</finish>
<size>10"</size>
</item>
<item>
<partNumber>06012663</partNumber>
<finish>Chrome</finish>
<size>14"</size>
</item>
<item>
<partNumber>06012665</partNumber>
<finish>Chrome</finish>
<size>16"</size>
</item>
<item>
<partNumber>06012666</partNumber>
<finish>Black</finish>
<size>16"</size>
</item>
<item>
<partNumber>06012667</partNumber>
<finish>Chrome</finish>
<size>18"</size>
</item>
<item>
<partNumber>06012668</partNumber>
<finish>Black</finish>
<size>18"</size>
</item>
<item>
<partNumber>06012669</partNumber>
<finish>Chrome</finish>
<size>20"</size>
</item>
<item>
<partNumber>06012670</partNumber>
<finish>Black</finish>
<size>20"</size>
</item>
<item>
<partNumber>06013460</partNumber>
<finish>Black</finish>
<size>10"</size>
</item>
<item>
<partNumber>06013461</partNumber>
<finish>Black</finish>
<size>12"</size>
</item>
<item>
<partNumber>06013462</partNumber>
<finish>Black</finish>
<size>14"</size>
</item>
<item>
<partNumber>06013463</partNumber>
<finish>Black</finish>
<size>16"</size>
</item>
<item>
<partNumber>06012662</partNumber>
<finish>Black</finish>
<size>12"</size>
</item>
<item>
<partNumber>06012661</partNumber>
<finish>Chrome</finish>
<size>12"</size>
</item>
<item>
<partNumber>06012659</partNumber>
<finish>Chrome</finish>
<size>10"</size>
</item>
<item>
<partNumber>06012664</partNumber>
<finish>Black</finish>
<size>14"</size>
</item>
<item>
<partNumber>06010612</partNumber>
</item>
<item>
<partNumber>06011482</partNumber>
<finish>Black</finish>
</item>
</items>
已添加:
这里是纯 XSLT 1.0 中的相同内容,使用递归命名模板而不是扩展函数:
XSLT 1.0
<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:template match="/root">
<items>
<xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
<item>
<xsl:copy-of select="partNumber"/>
<!-- 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>
</item>
</xsl:for-each>
</items>
</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>