SanjiSqurt
SanjiSqurt

Reputation: 87

Change attribute value in a for loop in XSLT

Hello I have an XML file with test steps like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<test>
    <card>
        <Description>data</Description>
        <Result>data</Result>
    </card>
    <card>
        <Description>data</Description>
        <Result>data</Result>
    </card>
    ...
</test>

And im using an XSLT to transform it like that:

<?xml version="1.0" encoding="UTF-8"?>
<Project name="project_name">
     <TestSteps>
          <TestStep uid="1">
                ...
          <TestStep/>
          <TestStep uid="2">
                ...
          <TestStep/>
                 ...
     <TestSteps/>
<Project/>

My XSLT looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="test">
        <Project name="project_name">
            <TestSteps>
            <xsl:for-each select="card">
                <xsl:variable name="i" select="position()" />
                    <TestStep uid="$i">
                    ...
                    <TestStep/>
                 ...
             <TestSteps/>
         <TestSuite/>
   <Tsr/>
<Project/>

As you can see I want to change the property "uid" of my TestStep at each iteration of the loop but with my method it doesn't work it just print me:

<TestSteps>
    <TestStep uid="$i">
        ...
    <TestStep/>
<TestSteps/>

Does someone has a solution to change that property each step? Thank you.

Upvotes: 0

Views: 142

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

Instead of:

<TestStep uid="$i">

use:

<TestStep uid="{$i}">

or simply:

<TestStep uid="{position()}">

For explanation, see:
https://www.w3.org/TR/1999/REC-xslt-19991116/#attribute-value-templates

Upvotes: 1

Related Questions