0

我有一个如下所示的 XML -

   <root>
    <row>
     <col1>16</col1>
      <col2>466</col2>
        <col3>144922</col3>
        <col4>0</col4>
        <col5>5668</col5>
        <col6>475</col6>
    </row>
</root>

根元素内的列数可以变化。它也可以达到 col9。我的要求是修改最后一列并像传入 XML 一样复制其他列。

到目前为止,我有类似的东西,我将值分配为变量中的最后一个元素,然后在到达最后一个位置时尝试调用它-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    version="2.0">

    <xsl:param name="line88.field2" />
    <xsl:param name="rec16.col2" />

    <xsl:variable name="col3">
        <xsl:choose>
            <xsl:when test="$rec16.col2 ='165'">
                <xsl:value-of select="'Y'"/>
            </xsl:when>
            <xsl:when>
             ------
            <xsl:when>
        </xsl:choose>

    </xsl:variable>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"></xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="row[position() = last()]">
        <col9>
            <xsl:call-template name="AnotherTemplate">
                <xsl:with-param name="inputData">
                    <xsl:value-of select="$col3" />
                </xsl:with-param>
            </xsl:call-template>
        </col9>
    </xsl:template>
    <xsl:template name="AnotherTemplate">
        <xsl:param name="inputData"></xsl:param>
        <xsl:value-of select="$inputData" />
    </xsl:template>

</xsl:stylesheet>

但这对我不起作用。只给我一列修改后的值。请帮忙。所需的结果应如下所示,其中最后一列具有变量的值。

<root>
<row>
        <col1>16</col1>
        <col2>466</col2>
        <col3>144922</col3>
        <col4>0</col4>
        <col5>5668</col5>
        <col6>Y</col6>
 </row>
</root>
4

1 回答 1

0

在不知道整个 XSLT 代码的情况下。您可以使用此row模板:

<xsl:template match="row/*[starts-with(local-name(),'col') and position() = last()]">
    <xsl:element name="{concat('col',position() div 2)}">
        <xsl:call-template name="AnotherTemplate">
            <xsl:with-param name="inputData">
                <xsl:value-of select="$col3" />
            </xsl:with-param>
        </xsl:call-template>
    </xsl:element>
</xsl:template>

col?它用给定的值(代码的结果)替换最后一个元素xsl:call-template

于 2020-06-01T10:04:54.733 回答