首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT2.0修改重复x次的节点的子节点

XSLT2.0修改重复x次的节点的子节点
EN

Stack Overflow用户
提问于 2020-09-21 11:20:55
回答 1查看 37关注 0票数 1

我试图复制“产品”节点x次。X是在带有属性的子节点“定制属性”(name="ProductUnit")中定义的值节点数量。到目前为止,我能够对其进行存档,但现在我希望更改每个带有后缀的复制"product“节点的节点"sku”。

后缀应该添加“product”(603890001_ST - 603890001_PK;20等)中的值。

在这里,我遇到了我的问题,因为我只想将这个sku更改应用到我复制的“产品”中,而不是在gerneral中,因为可能有一些产品节点没有带有属性的“自定义属性”(name="ProductUnit"),因此只复制了1:1,此外,值也可能会改变。

到目前为止这是我的代码:http://xsltransform.net/eixk6RM/3

输入XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<enfinity xmlns="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns1="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex" xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt" major="6" minor="1" branch="enterprise" xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" build="build" family="enfinity">
    <product sku="603890001" import-mode="REPLACE">
        <sku>603890001</sku>
        <name xml:lang="de-AT">TEST</name>
        <custom-attributes>
            <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
            <custom-attribute name="ProductUnits" dt:dt="string">
                <value>ST</value>
                <value>PG;20</value>
                <value>KA;10</value>
            </custom-attribute>
        </custom-attributes>
    </product>
    <product sku="12345" import-mode="REPLACE">
        <sku>12345</sku>
        <name xml:lang="de-AT">TEST</name>
        <custom-attributes>
            <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
        </custom-attributes>
    </product>
</enfinity>

输出XML (现在):

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?><enfinity xmlns="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns1="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex" xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt" major="6" minor="1" branch="enterprise" xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" build="build" family="enfinity">
    <product sku="603890001" import-mode="REPLACE">
        <sku>603890001</sku>
        <name xml:lang="de-AT">TEST</name>
        <custom-attributes>
            <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
            <custom-attribute name="ProductUnits" dt:dt="string">
                <value>ST</value>
                <value>PG;20</value>
                <value>KA;10</value>
            </custom-attribute>
        </custom-attributes>
    </product><product sku="603890001" import-mode="REPLACE">
        <sku>603890001</sku>
        <name xml:lang="de-AT">TEST</name>
        <custom-attributes>
            <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
            <custom-attribute name="ProductUnits" dt:dt="string">
                <value>ST</value>
                <value>PG;20</value>
                <value>KA;10</value>
            </custom-attribute>
        </custom-attributes>
    </product><product sku="603890001" import-mode="REPLACE">
        <sku>603890001</sku>
        <name xml:lang="de-AT">TEST</name>
        <custom-attributes>
            <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
            <custom-attribute name="ProductUnits" dt:dt="string">
                <value>ST</value>
                <value>PG;20</value>
                <value>KA;10</value>
            </custom-attribute>
        </custom-attributes>
    </product>
    <product sku="12345" import-mode="REPLACE">
        <sku>12345</sku>
        <name xml:lang="de-AT">TEST</name>
        <custom-attributes>
            <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
        </custom-attributes>
    </product>
</enfinity>

输出XML (需要):

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?><enfinity xmlns="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns1="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex" xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt" major="6" minor="1" branch="enterprise" xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" build="build" family="enfinity">
<product sku="603890001_ST" import-mode="REPLACE">
    <sku>603890001</sku>
    <name xml:lang="de-AT">TEST</name>
    <custom-attributes>
        <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
        <custom-attribute name="ProductUnits" dt:dt="string">
            <value>ST</value>
            <value>PG;20</value>
            <value>KA;10</value>
        </custom-attribute>
    </custom-attributes>
</product><product sku="603890001_PG;20" import-mode="REPLACE">
    <sku>603890001</sku>
    <name xml:lang="de-AT">TEST</name>
    <custom-attributes>
        <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
        <custom-attribute name="ProductUnits" dt:dt="string">
            <value>ST</value>
            <value>PG;20</value>
            <value>KA;10</value>
        </custom-attribute>
    </custom-attributes>
</product><product sku="603890001_KA;10" import-mode="REPLACE">
    <sku>603890001</sku>
    <name xml:lang="de-AT">TEST</name>
    <custom-attributes>
        <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
        <custom-attribute name="ProductUnits" dt:dt="string">
            <value>ST</value>
            <value>PG;20</value>
            <value>KA;10</value>
        </custom-attribute>
    </custom-attributes>
</product>
<product sku="12345" import-mode="REPLACE">
    <sku>12345</sku>
    <name xml:lang="de-AT">TEST</name>
    <custom-attributes>
        <custom-attribute name="Farbe" dt:dt="string">gelb</custom-attribute>
    </custom-attributes>
</product>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-21 12:00:56

我相信这应该能做你想做的事--但我不打算比较143行代码来确保:

XSLT2.0

代码语言:javascript
复制
<xsl:stylesheet version="2.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex">

<!-- identity transform (for all modes) -->
<xsl:template match="@*|node()" mode="#all">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="#current"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="product[custom-attributes/custom-attribute/@name='ProductUnits']">
    <xsl:for-each select="custom-attributes/custom-attribute[@name='ProductUnits']/value">
        <xsl:apply-templates select="ancestor::product" mode="multi">
            <xsl:with-param name="unit" tunnel="yes" select="."/>
        </xsl:apply-templates>
    </xsl:for-each>
</xsl:template>

<xsl:template match="sku" mode="multi">
    <xsl:param name="unit" tunnel="yes"/>
    <xsl:copy>
        <xsl:value-of select="."/>
        <xsl:text>_</xsl:text>
        <xsl:value-of select="$unit"/>
    </xsl:copy>
</xsl:template>

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

https://stackoverflow.com/questions/63991235

复制
相关文章

相似问题

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