简体   繁体   中英

XML to XSLT - Question - Repeating XML Elements

I have data that comes in from the XML like this....

   <DetailLine><FormField><Name>Vendor Item</Name> 
   <DisplayValue>0201029</DisplayValue><Value /><Value2 /> 
   </FormField><FormField><Name>Item Description</Name> 
   <DisplayValue>BOX JUNCTION HF AHC5111 10X10X</DisplayValue> 
   <Value>BOX JUNCTION HF AHC5111 10X10X</Value><Value2 />
    <DetailLine><FormField><Name>Vendor Item</Name> 
   <DisplayValue>0201040</DisplayValue><Value /><Value2 /> 
   </FormField><FormField><Name>Item Description</Name> 
   <DisplayValue>BOX JUNCTION HF A201608LP 20 X</DisplayValue> 
   <Value>BOX JUNCTION HF A201608LP 20 X</Value><Value2 />

I can code it in the XSLT like this, and as long as I know exactly how many lines I have, it works....

<xsl:for-each 
  select="GeneratedDocument/Detail/DetailLine[10]/FormField">
<td>
<xsl:value-of select="DisplayValue"/>
</td>
</xsl:for-each>

But I want it to be smarter and just say for each detail line, put 10 display values and go to the next row.

Any ideas? Thanks.

I want to return all the display values into each column and create a new row for each new detail line.

Each detail line has fixed 10 display values.

Sorry for all edits. New here.

Hope this code produces the output:

<xsl:template match="GeneratedDocument">
    <table>
        <xsl:for-each select="Detail/DetailLine">
            <tr>
                <xsl:for-each select="FormField">
                    <td>
                        <xsl:value-of select="DisplayValue"/>
                    </td>
                </xsl:for-each>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

This code creates a table element and uses two nested "for-each" loops. The outer loop iterates over each "DetailLine" element within the "Detail" element. For each "DetailLine" element, the inner loop iterates over each "FormField" element within the "DetailLine" element, and creates a table cell (td element) with the "DisplayValue" element of the current "FormField" element. The inner loop will run for each "FormField" element and will create a new column for each one of them. The outer loop will run for each "DetailLine" element and will create a new row for each one of them. This way, it will have 10 columns for each row, and the "DisplayValue" element will be the content of each column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM