Reputation: 21
I'm working with an XML file, that I know has to best layout. But unfortunately I can't changed it. Is it possible to build an call-template part and check on SupplierName and the position of my for-each. I've tried some things (like below) but none of them is working. The first parameter gives only the value SupplierName1, SupplierName2 or SupplierName3. The second gives unfortunately an error. The third option also doesn't work, but I found out that that is not possible to use how I tried it. What I expect to get is Name1, Test2 and Qwerty and not SupplierName1, SupplierName2 or SupplierName3. I hope there is somebody that can help me with this issue.
Things I've tried
<xsl:with-param name="test">SupplierName<xsl:value-of select="position()"/></xsl:with-param><xsl:with-param name="test2"><xsl:value-of select="SupplierName + position()" /></xsl:with-param><xsl:with-param name="test3"><xsl:value-of /><xsl:attribute name="select">SupplierName<xsl:value-of select="position()" /></xsl:attribute></xsl:with-param>
Example of my XML-file
<Result xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ContactGuid>8fb6415caa2c7c98dde69734dc2b715d</ContactGuid>
<Deals>
<Item>
<SupplierName1>Name1</SupplierName1>
<Costpermonth1>18,62</Costpermonth1>
<SupplierName2>Test2</SupplierName2>
<Costpermonth2>19,67</Costpermonth2>
<SupplierName3>Qwerty</SupplierName3>
<Costpermonth3>20,03</Costpermonth3>
</Item>
</Deals>
</Result>
Example of my XSLT file
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="Result/Deals/Item"/>
</xsl:template>
<xsl:template match="Item">
<xsl:variable name="supp1_name"><xsl:value-of select="SupplierName1" /></xsl:variable>
<xsl:variable name="supp2_name"><xsl:value-of select="SupplierName2" /></xsl:variable>
<xsl:variable name="supp3_name"><xsl:value-of select="SupplierName3" /></xsl:variable>
<xsl:variable name="alg_count"><xsl:value-of select="count(SupplierName1) + count(SupplierName2) + count(SupplierName3)"/></xsl:variable>
<xsl:for-each select="(//*)[position() <= $alg_count]">
<xsl:call-template name="top3_row">
<xsl:variable name="pos"><xsl:value-of select="position()" /></xsl:variable>
<xsl:with-param name="test">SupplierName<xsl:value-of select="position()"/></xsl:with-param>
<xsl:with-param name="test2"><xsl:value-of select="SupplierName + position()" /></xsl:with-param>
<xsl:with-param name="test3"><xsl:value-of /><xsl:attribute name="select">SupplierName<xsl:value-of select="position()" /></xsl:attribute></xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="top3_row">
<xsl:param name="test"/>
<xsl:param name="test2"/>
# <xsl:value-of select="$test" /> / <xsl:value-of select="$test2" /><br/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Views: 407
Reputation: 167716
You haven't explained which result format (HTML?) you want and the input suggests the values are ordered by the number at the end thus if you simply process them with e.g.
<xsl:template match="Item">
<ol>
<xsl:apply-templates select="*[starts-with(local-name(), 'SupplierName')]"/>
</ol>
</xsl:template>
<xsl:template match="Item/*[starts-with(local-name(), 'SupplierName')]">
<li>
<xsl:value-of select="concat(., ' / ', following-sibling::*[1][starts-with(local-name(), 'Costpermonth')])"/>
</li>
</xsl:template>
you should get the right pairs of the original input order. The use of ol
and li
is just an example for HTML output, use table/tr/td
if you want table rows/cells or use just plain text without wrapping elements if that is your target format, the way you select items for processing doesn't depend on that.
Upvotes: 1