Help with combining alternate rows of 2 XML files and displaying it as one HTML table
I h开发者_运维技巧ave 2 XML files and I would like to display them in one HTML table having alternate rows from each file. I know of an approach using XSLT, can someone guide me on how this can be done?
carillonator has given you the first part, but the "whip up some xsl logic to alternate between the two inputs" part is not obvious. You need to do something like this:
<xsl:template match="/">
<xsl:variable name="file1" select="//foo"/>
<xsl:variable name="file2" select="document('c:\temp\myfile.xml')//foo"/>
<output>
<xsl:for-each select="$file1">
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="$file2[position() = $pos]/>
</xsl:for-each>
<xsl:apply-templates select="$file2[position() > count($file1)"/>
</output>
</xsl:template>
This alternates transforming foo
elements in the source document and foo
elements in the second file. If there are more foo
elements in the source document, the for-each loop will output them all; if there are more in the second file, the last line outside the for-each loop makes sure they get output.
If you want the output to stop copying once either of the lists runs out, change the select
in the for-each loop to $file1[not(position() > count($file2))]
, and omit the apply-templates
line outside of the for-each loop.
I'm not sure you actually need xslt to accomplish this.
You can create two methods where each one will parse a different xml file using a reader. Make sure you use a Yield to get each row.
Then in a method you can call CombineXml(), use a loop to go through an call each method in the loop. Thus you will get a row from each xml file and can combine then.
You will also have to add logic for disparate rows and boundary conditions etc.
XSLT allows only one XML document as standard input, but you can use the document()
function to call on another.
<xsl:apply-templates select="document('file2.xml')/foo" />
then just whip up some xsl logic to alternate between the two inputs
精彩评论