Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xsl:for-each... help needed 1

Status
Not open for further replies.

MehmetAli

Technical User
Sep 19, 2003
4
DE
Hello Everyone,

I am trying to convert an xml file that looks like this:
<fields>
<Table1>

<Name>Company01</Name>
<Name>Company02</Name>
<Name>Company03</Name>
....
<Name>Company_n</Name>

</Table1>
<Table2>

<Category>A</Category>
<Category>B</Category>
<Category>A</Category>
....
<Category>B</Category>

</Table2>
</fields>

into something like this:

<fields>
<Table>
<Name>Company01</Name>
<Category>A</Category>
<Name>Company02</Name>
<Category>B</Category>
<Name>Company03</Name>
<Category>A</Category>
......
<Name>Company_n</Name>
<Category>B</Category>
</Table>
</fields>

Each line in <Table1> corresponds to the line with the same number in <Table2>, i.e. first <Name> corresponds to the first <Category>, etc...

Any ideas?

Thanks in advance
Memo
 
I did not try to do this but at a glance it seems you might be able to process the <Name> elements and use their position() to generate a position predicate for the XPath statement to select the <Category> element. But like i said... just a guess. B-)

-pete
 
This is exactly what I am trying to achieve. If there were a limited number of <Names> and <Categories>, it would be as simple as writing the exact position number for each of these elements.
But we are talking about hundreds of names and categories, so the question is how do I manage to generate the position number?

Thanks again
 
>> so the question is how do I manage to generate the position number?

did you try using position)()?

-pete
 
hi,

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;
<xsl:template match=&quot;/&quot;>

<xsl:apply-templates/>


</xsl:template>


<xsl:template match=&quot;//fields&quot;>
<table border=&quot;1&quot;>


<xsl:for-each select=&quot;Table1/Name&quot;>

<xsl:variable name=&quot;pos&quot;>

<xsl:value-of select=&quot;position()&quot;/>

</xsl:variable>


<tr>

<td><xsl:value-of select=&quot;.&quot;/></td>

<xsl:for-each select=&quot;//Category&quot;>

<xsl:if test=&quot;position()=$pos&quot;>

<td><xsl:value-of select=&quot;.&quot;/></td>

</xsl:if>

</xsl:for-each>


</tr>

</xsl:for-each>


</table>

</xsl:template>


</xsl:stylesheet>
 
So does that work? Here is what i came up with that works with MSXML4:
Code:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?>
<xsl:stylesheet
	xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL]
	version=&quot;1.0&quot;>
	
<xsl:output omit-xml-declaration=&quot;no&quot;
             method=&quot;html&quot;
             doctype-public=&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
             doctype-system=&quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;[/URL]
	     indent=&quot;no&quot; encoding=&quot;UTF-8&quot; />

<xsl:template match=&quot;Name&quot;>
	<xsl:variable name=&quot;pos&quot; select=&quot;position()&quot;/>
	<Name><xsl:value-of select=&quot;.&quot;/></Name>
	<Category>
		<xsl:for-each select=&quot;//fields/Table2/Category[position() = $pos]&quot;>
		<xsl:value-of select=&quot;.&quot;/>
		</xsl:for-each>
	</Category>
</xsl:template>

<xsl:template match=&quot;/&quot;>
<fields><Table>
	<xsl:apply-templates select=&quot;//fields/Table1/Name&quot; />
</Table></fields>
</xsl:template>
</xsl:stylesheet

-pete
 
hi,

It works. I forget to add elements Name and category to the values. Sorry for that one.
 
Thanks everyone,

@Sethu27
Your code solved my problem. Thanks a million.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top