首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT1.0-使用Vanilla XSLT 1.0或PHP XSLT的节点中值唯一性的输出计数

XSLT1.0-使用Vanilla XSLT 1.0或PHP XSLT的节点中值唯一性的输出计数
EN

Stack Overflow用户
提问于 2020-12-23 04:58:36
回答 1查看 55关注 0票数 0

我的XML数据中有一个名为这个节点的节点,它包含一个唯一的值,比如45678。通过XSLT,如果在完整的数据集中不止一次(1)找到45678,我只想包含该节点的数据。最终结果可以修改现有节点,或者创建一个名为“新节点”的节点。此数据的最终结果将创建“带子的父”产品。productId值将所有类似的产品组合在一起,问题是,有时只有一个具有该键的产品,而我不应该创建一个父产品。

重要注意:为什么我这么挣扎,第一,我仍然是全新的XSL,第二,当前版本的引擎不允许我使用EXSLT,但它似乎我可以使用PHP,如果必要。

样本数据集

<productId>0243176820</productId> --您可以看到这个值被多次使用,在这种情况下,它应该包含在结果数据中,<productId>1136194258</productId>,但是这个值只包含一次。

以下样本数据

代码语言:javascript
复制
<root>
  <part>
    <partNumber>06013464</partNumber>
    <punctuatedPartNumber>0601-3464</punctuatedPartNumber>
    <partStatusDescription>DISCONTINUED</partStatusDescription>
    <partDescription>Flat Black 18" 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>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>

当前处理数据的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: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  &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>

                        <!-- 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>
                       
                        <!-- I understand this wouldn't work as I'm just checking the total nodes, but I think someonehow I need to set the value of this node as a variable, and then check the count of this variable. This is easy in PHP, but I don't fully understand XSL  -->
                        <xsl:if test="count(/root/part/configurable_id) &gt; 1">
                            <xsl:element name="configurable_id">
                                <xsl:value-of select="productId"/>
                            </xsl:element>
                        </xsl:if>

                    </xsl:element>
                </xsl:if>
            </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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-23 05:59:26

要检查XSLT1.0中值的唯一性,可以使用对门窗群的稍微修改。

下面是一个最小示例:

XSLT1.0

代码语言: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:key name="part-by-product" match="part" use="productId" />

<xsl:template match="/root">
    <items>
        <xsl:for-each select="part[supplierNumber/text() and partStatusDescription != 'DISCONTINUED']">
            <item>
                <xsl:copy-of select="partNumber | productId"/>
                <!-- check productId uniqueness -->
                <xsl:if test="count(key('part-by-product', productId)) > 1">
                    <configurable_id>
                        <xsl:value-of select="productId"/>
                    </configurable_id>
                </xsl:if>
            </item>
        </xsl:for-each>
    </items>        
</xsl:template>

</xsl:stylesheet>

应用于输入示例,这将返回:

结果

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<items>
  <item>
    <partNumber>06012660</partNumber>
    <productId>0243176820</productId>
    <configurable_id>0243176820</configurable_id>
  </item>
  <item>
    <partNumber>06012663</partNumber>
    <productId>0243176820</productId>
    <configurable_id>0243176820</configurable_id>
  </item>
  <item>
    <partNumber>06011482</partNumber>
    <productId>0635669078</productId>
  </item>
</items>

与你的问题无关:

注意使用谓词而不是xsl:if文字结果元素代替xsl:element来减少代码的数量。

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

https://stackoverflow.com/questions/65419245

复制
相关文章

相似问题

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