开发者

using position() function in xslt

开发者 https://www.devze.com 2023-01-31 05:27 出处:网络
<EmployeeDetails> <Employee> <Name>TEST</Name> </Employee> <Employee>
<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>

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">&l开发者_开发问答t;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>

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

and the output should be :

<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>

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


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>

Output:

<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>


Before your first <xsl:template>, add

<xsl:strip-space elements="*"/> 

This will get rid of the whitespace-only text nodes @khachik is referring to. Then your position counts should be what you expect.


This template generates what you need:

<xsl:template match="EmployeeDetails/Employee">
    <xsl:copy>
        <xsl:attribute name="id">
          <xsl:value-of select="count(preceding-sibling::Employee) + 1"/>
        </xsl:attribute>
    </xsl:copy>
</xsl:template>

Source information.

0

精彩评论

暂无评论...
验证码 换一张
取 消