cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT code

Former Member
0 Kudos

Hi ,

my source structure is



<?xml version="1.0" encoding="UTF-8"?>
<records>
	<field1>a</field1>
	<field2>b</field2>
	<field3>c</field3>
	<field4>d</field4>
..........................
..............................
,,,,,,,,,,,,,,
</records>

  

my output should be


<?xml version="1.0" encoding="UTF-8"?>
<output>
	<record>
		<data>field1</data>
		<value>a</value>
                                          <counter>1/<counter>
	</record>
	<record>
		<data>field2</data>
		<value>b</value>
                                          <counter>2/<counter>
	</record>
	<record>
		<data>field3</data>
		<value>c</value>
                                          <counter>3/<counter>
	</record>
	<record>
		<data>field4</data>
		<value>d</value>
                                          <counter>4/<counter>
	</record>
........................
.............................
..................
</output>

basically i need the syntax for...

1) looping each element and then passing the corresponding tag name and value

2) increament.

thnx

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

The following should do the trick:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/records">
    <output>
      <xsl:for-each select="*">
        <record>
          <data><xsl:value-of select="local-name(.)"/></data>
          <value><xsl:value-of select="."/></value>
        </record>
      </xsl:for-each>
    </output>
  </xsl:template>
</xsl:stylesheet>

Cheers, harald

rajasekhar_reddy14
Active Contributor
0 Kudos

Hi,

This is the sample code,to loop

<xsl:template match="/*">
		<xsl:for-each select="record">
			<data>value fo select filed 1(u need to select the first position value)</data>
                                                                <value><xsl:text>a</xsl:text></QUALF>
		</xsl:for-each>
	</xsl:template>

Regards,

Raj

Former Member
0 Kudos

Hi raja,

I need a genralised code,

select = "value of filed 1... here i want nth field also selecting the value of field1 is of

i can do that in java easily.... but wnat to do in XSLT.. i am a beginer in XSLT

Former Member
0 Kudos

Hi,

I think we need to use the java mapping or you can go with the simple graphical mapping by replicating the target structure and hard coding the fields names that come from source side.

Regards,

Sainath Chutke

Former Member
0 Kudos

sainath.. thats very bad solution...

i know this is very simple in XSLT... but i dont know the syntax...

Former Member
0 Kudos

Hey Sam,

Probably what you can do is:

Take a count of the tags for the number of times your target structure has to repeat.

Then create a different template for for loop.

Inside this ue " <xsl:if test="$i &lt;= $count"> " where $i is the begin of the loop and $Count if the max number for the loop to repeat.

Now you can use foreach tag of xslt to generate the target structure desired.

Code for ur ref:

<xsl:template name="for.loop">

<xsl:param name="i" />

<xsl:param name="count"/>

<xsl:if test="$i &lt;= $count">

<item>

<xsl:for-each select="The X-Path">

<xsl:variable name="XYZ">

<xsl:value-of select="ABC" />

</xsl:variable>

</xsl:for-each>

</item>

</xsl:template>

Suggestion: Enough info is available if you google with "using for loop in XSL"

Hope this hels.

Warm Regards,

Anshul

Edited by: Anshul Chowdhary on Sep 8, 2010 9:26 AM