Reputation: 1091
I am referring an xml file(document.xml), through document function. Based on the input xml id value, I am taking the corresponding value from document.xml. The output will have this corresponding value and the position in input, as shown in the actualoutput.xml. However, would like to generate an output xml like expectedOutput.xml, having the attributes Value. Could anyone please point how to change my xsl file to get the expectedOutput.xml?
below are the files XslFile.xsl
<xsl:stylesheet version="1.0" exclude-result-prefixes="msxsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<result>
<xsl:apply-templates select="/*/id"/>
</result>
</xsl:template>
<xsl:template match="id">
<xsl:variable name="currencydetails" select="document('document.xml')/doc"/>
<xsl:variable name="id">
<xsl:value-of select="."/>
</xsl:variable>
<mapValue>
<xsl:variable name="fromDocument" select="$currencydetails/id[@value=$id]"/>
<xsl:value-of select="$fromDocument"/>
</mapValue>
<position>
<xsl:value-of select="position()"/>
</position>
</xsl:template>
</xsl:stylesheet>
document.xml
<doc>
<id value="123">abc</id>
<id value="456">abc</id>
<id value="011">def</id>
<id value="rty">ghj</id>
<id value="iop">qwd</id>
<id value="321">ply</id>
</doc>
input.xml
<Root>
<id>123</id>
<id>321</id>
<id>897</id>
<id>011</id>
<id>456</id>
</Root>
actualoutput.xml - I am able to reach only till here
<result>
<mapValue>abc</mapValue>
<position>1</position>
<mapValue>ply</mapValue>
<position>2</position>
<mapValue/>
<position>3</position>
<mapValue>def</mapValue>
<position>4</position>
<mapValue>abc</mapValue>
<position>5</position>
</result>
expectedoutput.xml - what i want
<result>
<map Value="abc">
<position>1</position>
<position>5</position>
</map>
<map Value="nomatch">
<position>3</position>
</map>
<map Value="def">
<position>4</position>
</map>
<map Value="ply">
<position>2</position>
</map>
</result>
Thanks. I will read more about xsl:number and position() function
However, for the above solution, I get the below response. Could you please guide/help me. The output should be mapped on the basis of value of 'fromdocument'.
<result>
<map value="abc"/>
<position>1</position>
<map value="ply"/>
<position>6</position>
<map value=""/>
<position/>
<map value="def"/>
<position>3</position>
<map value="abc"/>
<position>2</position>
</result>
Upvotes: 1
Views: 161
Reputation: 163385
Change
<mapValue>
<xsl:variable name="fromDocument" select="$currencydetails/id[@value=$id]"/>
<xsl:value-of select="$fromDocument"/>
</mapValue>
to
<xsl:variable name="fromDocument" select="$currencydetails/id[@value=$id]"/>
<map value="{$fromDocument}"/>
Change
<position>
<xsl:value-of select="position()"/>
</position>
to
<position>
<xsl:number select="$fromDocument"/>
</position>
or in XSLT 1.0,
<position>
<xsl:for-each select="$fromDocument">
<xsl:number/>
</xsl:for-each>
</position>
(You're one of many people who has guessed wrongly what position() does without reading the spec).
And (a stylistic and performance improvement) change
<xsl:variable name="id">
<xsl:value-of select="."/>
</xsl:variable>
to
<xsl:variable name="id" select="."/>
Upvotes: 1