xsl中的循环节点属性值

时间:2015-02-19 07:24:30

标签: xml xslt

我需要帮助处理循环中的节点属性值。假设我有一个输入XML

    <?xml version="1.0" encoding="UTF-8"?>
<applicationConfiguration>
    <function name="Setting">
        <option name="interface" value="">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-1.address" value="AV:BC:B4:17:63:E0">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-2.address" value="AV:BC:B4:17:53:20">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-3.address" value="11:SD:B4:16:2F:D8">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-4.address" value="34:SD:B4:16:2F:D8">
            <configuration>FE</configuration>
        </option>
        <option name="sshTunnel" value="">
            <configuration>FE</configuration>
        </option>
    </function>
</applicationConfiguration>

必需的输出XML -

    <?xml version="1.0" encoding="UTF-8"?>
<applicationConfiguration>
    <function name="Setting">
        <option name="interface" value="abc">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-1.address" value="test">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-2.address" value="result">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-3.address" value="cdf">
            <configuration>FE</configuration>
        </option>
        <option name="0-0-4.address" value="34:SD:B4:16:2F:D8">
            <configuration>FE</configuration>
        </option>
        <option name="sshTunnel" value="tunn">
            <configuration>FE</configuration>
        </option>
    </function>
</applicationConfiguration>

想要使用单个模板实现此功能。我已尝试过以下xsl,但无法实现此功能

XSL:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes" />    
        <xsl:param name="count" select="0" />
        <xsl:variable name="i" select="'1'" />
        <xsl:variable name="address1" select="'test'" />
        <xsl:variable name="address2" select="'result'" />
        <xsl:variable name="address3" select="'cdf'" />
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
            </xsl:copy>
        </xsl:template>
        <xsl:template match="/">
        <xsl:call-template name="inct">
            <xsl:with-param name="count" select="3" />
        </xsl:call-template>
    </xsl:template>
        <xsl:template name="inct" match="option[@name=concat('0-0-',$i,'.address')]/@value"> 
        <xsl:param name="count"/>
        <xsl:param name="i" select="$count" />
        <xsl:param name="limit" select="$count+1" />
        <xsl:value-of select="$count"/>
        <option value="${concat('address',$i,)}">
        </property> 
        <xsl:if test="$count > 1">      
          <xsl:call-template name="inct">               
                <xsl:with-param name="count" select="$count - 1" />
                <xsl:with-param name="limit" select="$limit" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template> 
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

回复您编辑过的问题:

如果看起来只是想将type属性中的值复制到value属性,则可以通过以下方式执行此操作 very

XSLT 1.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:strip-space elements="*"/>

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

<xsl:template match="property/@value">
    <xsl:attribute name="value">
        <xsl:value-of select="../@type"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

编辑:

  

如果我将所需的输出XML更改为:<?xml version="1.0" encoding="UTF-8"?> <root> <property type="0-0-1.address" value="address1">value1</property> <property type="0-0-2.address" value="address2">value4</property> <property type="0-0-3.address" value="address3">value2</property> </root>

通过将上面的第二个模板替换为:

,可以轻松完成
<xsl:template match="property">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:attribute name="value">
            <xsl:value-of select="concat('address', position())"/>
        </xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>