xslt - XSL - iterate through elements and update based on the node index from another xml file -


i have xml file multiple shape elements each child material element contains code attribute. want update code attribute each material element based on value obtained separate xml file. problem have update source file have multiple elements.

here main xml file - main.xml:

<?xml version="1.0"?> <!--top--> <top>   <shapes>     <!--shape-->     <shape code="penisola" description="rectangle">       <!--material-->       <material code="mat01000257" ></material>     </shape>     <!--shape-->     <shape code="penisola" description="rectangle">       <!--material-->       <material code="mat01000258" ></material>     </shape>   </shapes>   <texts></texts> </top> 

...and update source file - updatesource.xml:

<?xml version="1.0"?> <job>   <job_number>b90512</job_number>   <job_address>2nd floor/ 28-32 albert road   vic 3205</job_address>   <benchtops>     <benchtop>       <material>material 1</material>     </benchtop>     <benchtop>       <material>material 2</material>     </benchtop>   </benchtops> </job> 

the desired outcome - output.xml:

<?xml version="1.0"?> <!--top--> <top>   <shapes>     <!--shape-->     <shape code="penisola" description="rectangle">       <!--material-->       <material code="material 1" ></material>     </shape>     <!--shape-->     <shape code="penisola" description="rectangle">       <!--material-->       <material code="material 2" ></material>     </shape>   </shapes>   <texts></texts> </top> 

i imagine need iterate through both main xml , update source xml, unsure how this. attempts far have succeeded in updating both code attributes value first material element in update source file.

some information; both main.xml file , updatesource.xml file have matching number of shape , benchtop elements respectively, however, number of each change each job process, solution need dynamically work through number of elements present.

an xsl transformation meets requirement may following:

<?xml version="1.0" encoding="utf-16" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">     <xsl:output method="xml"/>      <!-- update source file: -->     <xsl:param name="usource" select="'updatesource.xml'"/>      <xsl:template match="material">         <!-- material's position: -->         <xsl:variable name="pos">             <xsl:value-of select="count(preceding::material)+1"/>         </xsl:variable>          <xsl:copy>             <xsl:apply-templates select="@*"/>              <!-- update code-attribute according pos-th material in $usource -->              <xsl:attribute name="code">                 <xsl:value-of select="(document($usource)//material)[position()=$pos]"/>             </xsl:attribute>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template>      <xsl:template match="node()|@*">         <xsl:copy>             <xsl:apply-templates select="node()|@*"/>         </xsl:copy>     </xsl:template> </xsl:stylesheet> 

the main points check @ position material located in source. , pick corresponding 1 updatesoure.xml.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -