在 xslt 中使用 position() 函数 [英] using position() function in xslt

查看:33
本文介绍了在 xslt 中使用 position() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<EmployeeDetails>
    <Employee>
        <Name>TEST</Name>
    </Employee>
    <Employee>
        <Name>TEST</Name>
    </Employee>
    <Employee>
        <Name>TEST</Name>
    </Employee>
    <Employee>
        <Name>TEST</Name>
    </Employee>
    <Employee>
        <Name>TEST</Name>
    </Employee>
</EmployeeDetails>

我尝试使用 xslt 如下:

I tried using xslt as below :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xd"
    version="1.0">

    <xsl:template match="EmployeeDetails/Employee">
        <xsl:copy>
        <xsl:attribute name="id"><xsl:value-of select="position()"/></xsl:attribute>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

对于上面的 xslt,position() 的输出打印为 2,4,6,8,10.

For above xslt the output for position() is printing as 2,4,6,8,10.

并且输出应该是:

<EmployeeDetails>
    <Employee id="1">
        <Name>TEST</Name>
    </Employee>
    <Employee id="2">
        <Name>TEST</Name>
    </Employee>
    <Employee id="3">
        <Name>TEST</Name>
    </Employee>
    <Employee id="4">
        <Name>TEST</Name>
    </Employee>
    <Employee id="5">
        <Name>TEST</Name>
    </Employee>
</EmployeeDetails>

如何将 id 属性打印为 1,2,3.... 之类的序列.

How to print as a sequence like 1,2,3.... for id attribute.

推荐答案

xsl:number 指令正是为此任务制定的:

The xsl:number instruction was made precisely for this task:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="Employee">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:attribute name="id">
                <xsl:number/>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

输出:

<EmployeeDetails>
    <Employee id="1">
        <Name>TEST</Name>
    </Employee>
    <Employee id="2">
        <Name>TEST</Name>
    </Employee>
    <Employee id="3">
        <Name>TEST</Name>
    </Employee>
    <Employee id="4">
        <Name>TEST</Name>
    </Employee>
    <Employee id="5">
        <Name>TEST</Name>
    </Employee>
</EmployeeDetails>

这篇关于在 xslt 中使用 position() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆