XSLT looping variable number of times across multiple segments

Here should go questions about transforming XML with XSLT and FOP.
vbejugam
Posts: 1

XSLT looping variable number of times across multiple segments

Wed Feb 13, 2019 10:42 pm

I am very new to XSLT coding and I am looking help on copying XML file something like below. Source and Destination are same XML format as both are using the same XSD file. Please help me how to map this XML file. If I am using foreach loop every time it copied all the TimingAndDuration . So in our case TimingAndDuration copied 15 times instead of 5 times.

Please help me on how to code this scenario.

Code: Select all

<Instruction>TextValue1</<Instruction>>

<!-- Below 2 segments can be repeated 0 to 40 times  I showed only 2 times below START -->
<MultipleTiming>TextValue2</MultipleTiming>
<TimingAndDuration>
   <Field1>FieldVal_1</Field1>
   <Field2>FieldVal_2</Field2>
   <Field3>FieldVal_3</Field3>
</TimingAndDuration>
<MultipleTiming>TextValue21</MultipleTiming>
<TimingAndDuration>
   <Field1>FieldVal_11</Field1>
   <Field2>FieldVal_22</Field2>
   <Field3>FieldVal_33</Field3>
</TimingAndDuration>
<!-- Below 2 segments can be repeated 0 to 40 times END -->

<Administration>TextValue2</Administration>
<DoseAdministration>TextValue3</DoseAdministration>
<TimingAndDuration>
   <Field1>FieldVal_4</Field1>
   <Field2>FieldVal_5</Field2>
   <Field3>FieldVal_6</Field3>
 </TimingAndDuration>
 
 <!-- Below 2 segments can be repeated 0 to 40 times  I showed only 2 times below  START -->
<MultipleTiming>TextValue4</MultipleTiming>
<TimingAndDuration>
   <Field1>FieldVal_7/Field1>
   <Field2>FieldVal_8</Field2>
   <Field3>FieldVal_9</Field3>
</TimingAndDuration>
<MultipleTiming>TextValue41</MultipleTiming>
<TimingAndDuration>
   <Field1>FieldVal_71/Field1>
   <Field2>FieldVal_81</Field2>
   <Field3>FieldVal_91</Field3>
</TimingAndDuration>
<!-- Below 2 segments can be repeated 0 to 40 times END -->
Radu
Posts: 5992

Re: XSLT looping variable number of times across multiple segments

Thu Feb 14, 2019 9:45 am

Hi,

If you want to change parts of an XML document, your XSLT needs to contain first of all the copy-template which is this one:

Code: Select all

<xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>


and in addition you can add xsl:templates to match certain nodes and change them in certain ways:

https://stackoverflow.com/questions/5876382/using-xslt-to-copy-all-nodes-in-xml-with-support-for-special-cases

Most people using xsl:for-each in XSLT are doing so because they come from a background of procedural programming. With XSLT most of the work can be done with matching elements using templates.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 2 guests