Search code examples
xmlxsltxfdf

XSLT for each (group?) with sorted two elements named as Code and Value


i need to transfer file xml to xfdf. And I have group with two element types code and value. Code is defining name of field and value is value of field. XML source is like this:

 <urn:identifications>
  <urn:identificationCode>dateOfBirth</urn:identificationCode>
  <urn:identificationValue>25021965</urn:identificationValue>
  <urn:identificationCode>ičdph</urn:identificationCode>     <!-- IC DPH -->
  <urn:identificationValue>1234567890</urn:identificationValue>
  <urn:identificationCode>ičo_sk</urn:identificationCode>      <!-- ICO (SK) -->
  <urn:identificationValue>0987654333</urn:identificationValue>
  <urn:identificationCode>ic</urn:identificationCode>      <!-- ICO (CZ) -->
  <urn:identificationValue>0987654321</urn:identificationValue>
  ...
</urn:identifications>

And I need something like this by XSLT to XFDF.

<identifications>
 <field name="dateOfBirth">
   <value>25021965</value>
 </field>
 <field name="ičdph">
   <value>1234567890</value>
 </field>
 <field name="ičo_sk">
   <value>0987654333</value>
 </field>
 <field name="ic">
   <value>0987654321</value>
 </field>
  ...
</identifications>

What I need to use? How? for-each-group? or som sets? Thank you very much.


Solution

  • If they always come in ordered pairs as shown in your example, then you could do simply:

    <xsl:template match="urn:identifications">
        <identifications>
            <xsl:for-each select="urn:identificationCode">
                <field name="{.}">
                    <value>
                        <xsl:value-of select="following-sibling::urn:identificationValue[1]"/>
                    </value>
                </field>
            </xsl:for-each>
        </identifications>
    </xsl:template>