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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load XSL by drop-down menu

Status
Not open for further replies.

bcfraser

Technical User
Sep 18, 2009
1
CA
Hi all,

I am fairly new to XML in this sense. I am trying to load an XML file to HTML and add some sort of filtering functionality using XSL.

I have the following code that is working:
Code:
<HTML>
<head>
</head>
<BODY>

<script type="text/javascript">

function Apply_XSLT()
{
  // Load XML file
  var XMLDoc = new ActiveXObject("Msxml2.DOMDocument.3.0")
  XMLDoc.async = false
  XMLDoc.load("Subcontracts.xml")
	
  // Load XSLT file
  var XSLTDoc = new ActiveXObject("Msxml2.DOMDocument.3.0")
  XSLTDoc.async = false
  XSLTDoc.load("documentNumberSort.xsl")

  document.all.XSLTOutput.innerHTML = XMLDoc.transformNode(XSLTDoc)
}


function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=anchors.length-1; i>=0; i--) {
var anchor = anchors[i];
if (anchor.href && anchor.href.substr(0,7) == "[URL unfurl="true"]http://")[/URL]
anchor.target = "_blank";
}
}
window.onload = externalLinks;

// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("Subcontracts.xml")

// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("documentNameSort.xsl")

// Transform
document.write(xml.transformNode(xsl))

function selected(){
for (i=0;i<document.getElementsByTagName('select').length; i++) {
document.getElementsByTagName('select').item(i).selectedIndex=0;
}
}

</script>
<input type="button" value="Apply XSLT" onClick="Apply_XSLT()"/>
<div id="XSLTOutput"></div>


</body>
</html>
documentnameSort.xsl source:
Code:
<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/TR/WD-xsl">[/URL] 
<xsl:template match="/">
<html>
<body> 
<table border="2" bgcolor="red"> 
<tr> 
<th>Document Number</th> 
<th>Document Name</th>
<th>Function</th>
<th>Document Type</th>
<th>Activity</th>
</tr> 
<xsl:for-each select="documents/data" order-by="+ docName"> 
<tr> 
<td> 
<xsl:value-of select="docID" /> 
</td> 
<td>
<a><xsl:attribute name="href">
<xsl:value-of select="docLink"/></xsl:attribute>
<xsl:value-of select="docName"/>
</a>
</td>
<td> 
<xsl:value-of select="function" /> 
</td>
<td> 
<xsl:value-of select="docType" /> 
</td>
<td> 
<xsl:value-of select="activity" />
</td>
</tr> 
</xsl:for-each> 
</table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet>

Ultimately what I would like to do is have a drop-down menu for flitering purposes (for example filter on the document number documentNumberSort.xsl) to load a XSLT file to replace what I have displayed already in the documentNameSort.xsl file.

I currently have a button that loads the documentNumberSort.xsl but it adds the information below the existing information.

I know it is probably something that is easy to do but since my programming knowledge is low I am having a hard time with it.

Thanks everyone.
Brent
 
[1] If you want to make xsl document behave differently according to user run-time input, you have to devise an alternative mechanism, basically a sort of passing some parameters to it via some addParameter method. Exact detail is implementation-specific. But the flexibility is there built-in the xslt recommendation. For php, you can refer to a recent thread for the general idea:

[2] In msxml2, you have to use a more versatile object model, namely, via Msxml2.XSLTemplate and Msxml2.FreeThreadedDOMDocument. Then the addParameter() is supported making the passing of parameters at run-time possible.

[3] To have [2] work, you have to make the inter-related change(s) to the xsl document as well.

[3.1] The change that comes immediately to notice is to have some top-level parameter defined in it.

[3.2] But [3.1] is not sufficient. Looked at your xsl document, I notice you are still using a deprecated namespace for xsl, [ignore][/ignore]. It is still supported by ie, but it won't have much to do for the future. You have to use [ignore][/ignore] xsl version 1.0 recommendation to work with ie and more versatile functionality.

[3.3] You can switch to that namespace, only now you have to change the line on with order-by. Take it out and write a line of <xsl:sort> tag thereafter is what you've to do.

[4] Hence, there are plenty to change and to understand there interactions in a go. The effort to make is on your side.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top