Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge Multiple XML then Merge with XSL

Status
Not open for further replies.

bont

Programmer
Sep 7, 2000
200
US
I have multiple XML with the same schema, let's say:
Code:
<?xml version="1.0" encoding="utf-8"?>
<CDs>
  <CD>..</CD>
  <CD>..</CD>
  <CD>..</CD>
</CDs>

and

<?xml version="1.0" encoding="utf-8"?>
<CDs>
  <CD>..</CD>
  <CD>..</CD>
  <CD>..</CD>
</CDs>

What I would like to do is merge them into 1 XML document prior to transforming them using ASP and XSL template. Here is my code so far:

Code:
Dim objSource
Dim objTarget
Dim objTemplate, objProcessor, strSortField, strGameNumber, objXML, objXSL, strHTML

Set objSource= Server.CreateObject("Msxml2.FreeThreadedDOMDocument.3.0")
Set objTarget= Server.CreateObject("Msxml2.FreeThreadedDOMDocument.3.0")
objSource.async = false
objTarget.async = false

objTarget.loadXML ("<AllCats />")

    If objSource.Load(Server.MapPath("LPz_Cat_0 - 9.xml")) Then
        For Each objNode In objSource.documentElement.childNodes
            objTarget.documentElement.appendChild objNode
        Next
    End If
    If objSource.Load(Server.MapPath("LPz_Cat_A.xml")) Then
        For Each objNode In objSource.documentElement.childNodes
            objTarget.documentElement.appendChild objNode
        Next
    End If


	Set objXSL = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.3.0")
	objXSL.Load Server.MapPath("lpz_all.xsl")
	Set objTemplate = Server.CreateObject("MSXML2.XSLTemplate.3.0")
	Set objTemplate.stylesheet = objXSL
	Set objProcessor = objTemplate.createProcessor
	objProcessor.input = objTarget
	objProcessor.Transform
strHTML = objProcessor.output
Response.write strHTML

I know it has problems, but I have been frustrated with it for quite a while. I believe it isn't too hard to combine them, but how do I get rid of the the doctypes (leaving 1), and then, what will my resulting XML be? I want it to look like this:

Code:
<?xml version="1.0" encoding="utf-8"?>
<CDs>
  <CD>..</CD>
  <CD>..</CD>
  <CD>..</CD>
  <CD>Second Set</CD>
  <CD>..</CD>
  <CD>..</CD>
</CDs>

Maybe I am missing something?
 
Why not use XSL to combine them:
Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.1" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <CDs>
      <xsl:copy-of select="CDs/CD"/>
      <xsl:copy-of select="document('LPz_Cat_A.xml')/CDs/CD"/>
    </CDs>
  </xsl:template>
</xsl:stylesheet>
 
I believe I found a solution. First I define an XML doc that has all the file names. I think I could have also just created a nodeset in my ASP:

Code:
<?xml version='1.0' encoding='utf-8' ?>
<LPFiles>
	<file href="LPz_Cat_A.xml" />
	<file href="LPz_Cat_B.xml" />	
	<file href="LPz_Cat_C.xml" />		
	<file href="LPz_Cat_D.xml" />	
	<file href="LPz_Cat_E.xml" />	
	<file href="LPz_Cat_F.xml" />	
	<file href="LPz_Cat_G.xml" />	
	<file href="LPz_Cat_H.xml" />	
	<file href="LPz_Cat_I.xml" />	
	<file href="LPz_Cat_J.xml" />	
	<file href="LPz_Cat_K.xml" />	
	<file href="LPz_Cat_L.xml" />	
	<file href="LPz_Cat_M.xml" />	
	<file href="LPz_Cat_N.xml" />	
	<file href="LPz_Cat_O.xml" />	
	<file href="LPz_Cat_P.xml" />	
	<file href="LPz_Cat_Q.xml" />	
	<file href="LPz_Cat_R.xml" />	
	<file href="LPz_Cat_S.xml" />	
	<file href="LPz_Cat_T.xml" />	
	<file href="LPz_Cat_U.xml" />	
	<file href="LPz_Cat_V.xml" />	
	<file href="LPz_Cat_W.xml" />	
<!--	<file href="LPz_Cat_X.xml" />	-->
	<file href="LPz_Cat_Y.xml" />	
<!--	<file href="LPz_Cat_Z.xml" />	-->
	<file href="LPz_Cat_0 - 9.xml" />	
	<file href="LPz_Cat_Soundtracks.xml" />	
	<file href="LPz_Cat_Various Artists.xml" />	
</LPFiles>

Next, I built a templated for the main page:
Code:
<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet version="1.0"  
	xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
	xmlns:msxsl="urn:schemas-microsoft-com:xslt" ><xsl:output method="html"/>
<xsl:include href="lpz_all_ind.xsl"/>
<xsl:template match="/">
	<xsl:variable name="doc.refs.1">
		<xsl:for-each select="LPFiles/file">
			<doc><xsl:value-of select="@href"/></doc>
		</xsl:for-each>
	</xsl:variable>

		<html>
			<head>
				<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
		        <xsl:element name="link">
		          <xsl:attribute name="rel">stylesheet</xsl:attribute>
		          <xsl:attribute name="href">../../styles/main.css</xsl:attribute>
		        </xsl:element>
		        <xsl:element name="link">
		          <xsl:attribute name="rel">stylesheet</xsl:attribute>
		          <xsl:attribute name="href">../../styles/mp3z.css</xsl:attribute>
		        </xsl:element>			        				
				<title>Our CD Collection</title>
				<script type="text/javascript" language="javascript" src="../../../scripts_js/bont_trees.js">

				</script> 
			</head>	
			<body>
				<script language="javascript" type="text/javascript">
					pageHeaders();
				</script>
				<div class="mainBody">
					<xsl:for-each select="msxsl:node-set($doc.refs.1)//doc" >
						<!-- Display product information using our DisplayProducts.xsl template -->
						<xsl:call-template name="LPzGroup" >
						<xsl:with-param name="atLPFile" select="document(.)"/>
						</xsl:call-template>
					</xsl:for-each>
				</div>				
			</body>				
		</html>
</xsl:template>
	
</xsl:stylesheet>

Notice that I put the XML above in a loop to call each file, as described in the message before this.

Finally, I defined a template for when each iteration occurs:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:output doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" doctype-system="[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd"[/URL] />
   <xsl:template name="LPzGroup" >
		<xsl:param name="atLPFile" select="." />
		
			<xsl:value-of select="$atLPFile//@cat" />

			<table border="0" cellpadding="0" cellspacing="0" class="mp3z_table">
				<tr class="mp3z_category">
					<td>
						Category: <xsl:value-of select="$atLPFile//@catName" />
					</td>
				</tr>
				<xsl:for-each select="$atLPFile//artist">
					<tr>
						<td class="mp3z_artist">Artist: <xsl:value-of select="@artistName" /></td>
					</tr>
					<xsl:for-each select="LP">
						<tr>
							<td class="mp3z_lp">
								<xsl:value-of select="@LPName" />
								<xsl:value-of select="@subLPName" />
							</td>																
						</tr>
					</xsl:for-each>
			</xsl:for-each>
		</table>
		<br />
   </xsl:template>
</xsl:stylesheet>

You can see the finished product here:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top