我有以下XML:

xml - XSLT-将值连接到现有值-LMLPHP

我希望id是subname + subname2的串联。示例:第一个ID为:ThinkVision,第二个ID为:LenovoMind

这可以通过xsl:for-each实现吗?

我尝试使用以下xslt:






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


<xsl:template match="/records/record/customList/customBucket[fn:lower-case(customName)='id']/customValue/text()">
    <xsl:for-each select="/records/record/customList">
        <xsl:value-of select="concat(customBucket[fn:lower-case(customName)='subname']/customValue/text(),customBucket[fn:lower-case(customName)='subname2']/customValue/text())"/>
    </xsl:for-each>
</xsl:template>




但是我得到的是每条记录的串联,而不是当前的:

xml - XSLT-将值连接到现有值-LMLPHP

样本XML:<records> <record> <customList> <customBucket> <customName>id</customName> <customValue>default</customValue> </customBucket> <customBucket> <customName>subname</customName> <customValue>Think</customValue> </customBucket> <customBucket> <customName>subname2</customName> <customValue>Vision</customValue> </customBucket> </customList> </record> <record> <customList> <customBucket> <customName>id</customName> <customValue>default</customValue> </customBucket> <customBucket> <customName>subname</customName> <customValue>Lenovo</customValue> </customBucket> <customBucket> <customName>subname2</customName> <customValue>Mind</customValue> </customBucket> </customList> </record> <record> <customList> <customBucket> <customName>id</customName> <customValue>default</customValue> </customBucket> <customBucket> <customName>subname</customName> <customValue>Think</customValue> </customBucket> <customBucket> <customName>subname2</customName> <customValue>Vision</customValue> </customBucket> </customList> </record> <record> <customList> <customBucket> <customName>id</customName> <customValue>default</customValue> </customBucket> <customBucket> <customName>subname</customName> <customValue>Mapped</customValue> </customBucket> <customBucket> <customName>subname2</customName> <customValue>Safe</customValue> </customBucket> </customList> </record> <record> <customList> <customBucket> <customName>id</customName> <customValue>default</customValue> </customBucket> <customBucket> <customName>subname</customName> <customValue>Sony</customValue> </customBucket> <customBucket> <customName>subname2</customName> <customValue>DCO</customValue> </customBucket> </customList> </record></records>

预期产量

<?xml version="1.0" encoding="UTF-8"?>
<records>
<record>
    <customList>
        <customBucket>
            <customName>id</customName>
            <customValue>ThinkVision</customValue>
        </customBucket>
        <customBucket>
            <customName>subname</customName>
            <customValue>Think</customValue>
        </customBucket>
        <customBucket>
            <customName>subname2</customName>
            <customValue>Vision</customValue>
        </customBucket>
    </customList>
</record>
<record>
    <customList>
        <customBucket>
            <customName>id</customName>
            <customValue>LenovoMind</customValue>
        </customBucket>
        <customBucket>
            <customName>subname</customName>
            <customValue>Lenovo</customValue>
        </customBucket>
        <customBucket>
            <customName>subname2</customName>
            <customValue>Mind</customValue>
        </customBucket>
    </customList>
</record>
</records>


==

场景2

输入:

<records>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Think</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Vision</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Lenovo</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Mind</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Think</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Vision</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Mapped</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Safe</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Sony</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>DCO</customValue>
            </customBucket>
        </customList>
    </record>
</records>


预期的输出

<records>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>ThinkVision</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Think</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Vision</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>LenovoMind</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Lenovo</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Mind</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>ThinkVision</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Think</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Vision</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>MappedSafe</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Mapped</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Safe</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>SonyDCO</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Sony</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>DCO</customValue>
            </customBucket>
        </customList>
    </record>
</records>


基本上,而不是覆盖“ id”,这里的想法是创建一个名为“ newid”的新节点,其逻辑与场景1相同:子名称+ subname2的串联。

xml - XSLT-将值连接到现有值-LMLPHP

场景2.1

预期输出(与方案2相同,但已重命名原始ID)

我添加了以下代码,但未重命名ID。

<xsl:param name="normRenamedID" select="'id_renamed'"/>

    <!-- Rename clientPortfolioID to RM_clientPortfolioID-->
    <xsl:template match="/customBucket[fn:lower-case(customName)='clientportfolioid']/customName/text()">
        <xsl:value-of select="$normRenamedID"/>
    </xsl:template>


<records>
    <record>
        <customList>
            <customBucket>
                <customName>id_renamed</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>ThinkVision</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Think</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Vision</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id_renamed</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>LenovoMind</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Lenovo</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Mind</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id_renamed</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>ThinkVision</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Think</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Vision</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id_renamed</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>MappedSafe</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Mapped</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>Safe</customValue>
            </customBucket>
        </customList>
    </record>
    <record>
        <customList>
            <customBucket>
                <customName>id_renamed</customName>
                <customValue>default</customValue>
            </customBucket>
            <customBucket>
                <customName>newid</customName>
                <customValue>SonyDCO</customValue>
            </customBucket>
            <customBucket>
                <customName>subname</customName>
                <customValue>Sony</customValue>
            </customBucket>
            <customBucket>
                <customName>subname2</customName>
                <customValue>DCO</customValue>
            </customBucket>
        </customList>
    </record>
</records>


xml - XSLT-将值连接到现有值-LMLPHP

最佳答案

当您这样做时:

<xsl:for-each select="/records/record/customList">


您要从customList根节点开始选择所有/元素。您当前的上下文在这里不起作用。

要获得所需的结果,请尝试以下方法:

<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="*"/>

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

<xsl:template match="customBucket[customName='id']/customValue">
    <xsl:copy>
        <xsl:value-of select="../../customBucket[customName='subname']/customValue" />
        <xsl:value-of select="../../customBucket[customName='subname2']/customValue" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>




添加:

对于您添加的方案,将第二个模板更改为:

<xsl:template match="customBucket[customName='id']">
    <xsl:copy-of select="."/>
    <customBucket>
        <customName>newid</customName>
        <customValue>
            <xsl:value-of select="../customBucket[customName='subname']/customValue" />
            <xsl:value-of select="../customBucket[customName='subname2']/customValue" />
        </customValue>
    </customBucket>
</xsl:template>




新增2:

对于其他方案,请将第二个模板更改为:

<xsl:template match="customBucket[customName='id']">
    <xsl:copy>
        <customName>
            <xsl:value-of select="$normRenamedID" />
        </customName>
        <xsl:apply-templates select="customValue"/>
    </xsl:copy>
    <customBucket>
        <customName>newid</customName>
        <customValue>
            <xsl:value-of select="../customBucket[customName='subname']/customValue" />
            <xsl:value-of select="../customBucket[customName='subname2']/customValue" />
        </customValue>
    </customBucket>
</xsl:template>

07-28 00:42