问题是,我有以XML格式存在的数据,但是,关键的数据片段不是在单独的节点中提供的。例如一件物品的大小,颜色,设备。虽然我通常会提供我尝试过的东西,但我甚至不知道从哪里开始。我已经找到了很多关于如何检查是否找到一个值的例子,但是没有找到如何获取匹配的值,然后在元素中返回它的例子。我只能使用XSLT1.0
这是一个关键的部分,我需要帮助,但在这篇文章中,我包括完整的样本数据和我当前的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>发布于 2020-12-21 21:45:57
这真是一团糟。
这很混乱,因为XSLT是为处理结构化数据而设计的,但是您的数据提供者选择不利用XML提供的结构。这里所需的处理类型将由regex更好地处理--然而,regex支持只在XSLT2.0中引入。
尽管如此,让我们看看能做些什么:
第一项任务是:
如果
<partDescription>包含黑色、扁平背,则contains应返回找到的值
相对容易:假设可能的完成选项是相互排斥的,您可以这样做:
<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>请注意,这还假定一个选项不包含在另一个选项中。如果不是这种情况,请确保在包含选项之前对包含选项进行测试。
现在关于尺寸,你说:
如果
<partDescription>包含一个数字,则获取该数字
从字面上看,你可以:
<xsl:variable name="size" select="translate(partDescription, translate(partDescription, '0123456789', ''), '')" />
<xsl:if test="$size">
<size>
<xsl:value-of select="$size"/>
</size>
</xsl:if>然而,这有两个缺陷:
partDescription中的所有数字连接在一起,即使它们不是同一个数字的一部分。一个更好(尽管还不完美)的解决方案将用空格字符标记描述,并寻找一个以数字开头的令牌。幸运的是,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>但是,如果描述包含多个以数字开头的单词,并且大小不是其中的第一个,这仍然会失败。
以下是这两项建议的最小实现:
XSLT1.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>当应用于输入示例时,这将返回:
结果
<?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>添加:
在纯XSLT1.0中,使用一个递归命名模板而不是扩展函数来实现同样的功能:
XSLT1.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>https://stackoverflow.com/questions/65397574
复制相似问题