xml - Loop selection with increment using XSLT -


i facing following issue:

if have following xml data:

<input>   <error>     <info>        <code> 111 </code>        <value>hello user </value>     </info>     <info>        <code>118</code>        <value>01</value>     </info>   </error>   <error>     <info>        <code> 111 </code>        <value>bye user </value>     </info>     <info>        <code>118</code>        <value>01</value>     </info>   </error>   <error>     <info>        <code> 111 </code>        <value>dead user </value>     </info>     <info>        <code>118</code>        <value>06</value>     </info>   </error>   <error>     <info>        <code> 111 </code>        <value>killed user </value>     </info>     <info>        <code>118</code>        <value>08</value>     </info>   </error> </input> 

how can build xslt transform above, knowing output should like

<recnum>"value found inside value[2]"</recnum> <error-description> "value found inside value[1]" </error-description> 

please note not have same output. sure of in every error element have 2 info elements , 2 value elements every time.

yet, don't know how many error elements have within same value[2], have 3 error elements, of them 2 info tags, 3 errors, info[2]/value/text() same.

so output above input should this

<errors>   <module>     <recnum>1 </recnum>     <error-description>hello user </error-description>     <error-description>by user </error-description>   </module>   <module>     <recnum>6 </recnum>     <error-description> dead user </error-description>   </module>   <module>     <recnum>8 </recnum>     <error-description> killed user </error-description>   </module> </errors> 

note didn't have value of 02, 03, 04, 05, 07, 09, etc ...

i had values 01, 06 & 08 these might differ. how can such logic done if please ?

i recommend try avoid xsl:for-each in favor of xslt in spirit use of templates:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:output omit-xml-declaration="yes" indent="yes"/>  <xsl:key name="kerrorbyval" match="error" use="*[2]/value"/>   <xsl:template match="/*">      <errors>        <xsl:apply-templates select=        "*[generate-id()=generate-id(key('kerrorbyval',*[2]/value)[1])]"/>      </errors>  </xsl:template>   <xsl:template match="error">   <module>    <recnum><xsl:value-of select="*[2]/value"/></recnum>    <xsl:apply-templates select="key('kerrorbyval',*[2]/value)/*[1]/value"/>   </module>  </xsl:template>   <xsl:template match="value">   <error-description><xsl:value-of select="."/></error-description>  </xsl:template> </xsl:stylesheet> 

when transformation applied on provided xml document:

<input>   <error>     <info>       <code> 111 </code>       <value>hello user </value>     </info>     <info>       <code>118</code>       <value>01</value>     </info>   </error>   <error>     <info>       <code> 111 </code>       <value>bye user </value>     </info>     <info>       <code>118</code>       <value>01</value>     </info>   </error>   <error>     <info>       <code> 111 </code>       <value>dead user </value>     </info>     <info>       <code>118</code>       <value>06</value>     </info>   </error>   <error>     <info>       <code> 111 </code>       <value>killed user </value>     </info>     <info>       <code>118</code>       <value>08</value>     </info>   </error> </input> 

the wanted, correct result produced:

<errors>    <module>       <recnum>01</recnum>       <error-description>hello user </error-description>       <error-description>bye user </error-description>    </module>    <module>       <recnum>06</recnum>       <error-description>dead user </error-description>    </module>    <module>       <recnum>08</recnum>       <error-description>killed user </error-description>    </module> </errors> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -