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

Is it possible to create an xml and xsl on the same file? 1

Status
Not open for further replies.

royboy75

Programmer
Feb 13, 2007
114
GB
Hello,

Is it possible to create an xml and xsl on the same file (instead 2 seperate files) and refer to the xsl part from the xml part?

Roy
 
I think I found a solution, it is working with a simple file but when I try to implement this on my more complicated one I get blank screen. I think that the select part of my <xsl:for-each tags are not parsed correctly.
Can someone please advice, here is my code in full:

Code:
<?xml version='1.0' encoding='UTF-8' standalone='no'?> 
<?xml-stylesheet type='text/xsl' href='test.xml'?>

<xsl:stylesheet xmlns:xsl='[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform'[/URL] 
				version='1.0' 
				xmlns:z="MyXMLStuff" 
				exclude-result-prefixes="z">
<xsl:output method='html'/>
<xsl:template match='z:MappingData'>
<html>
<body>
<table border='1'>
<tr>
<xsl:for-each select='/z:MappingData/z:MappingDataElement[1]/*'>
<th bgcolor='#66FF00'><xsl:value-of select='local-name()'/></th>
</xsl:for-each>
</tr>
<xsl:for-each select='/z:MappingData/z:MappingDataElement'>
<tr>
<xsl:for-each select='*'>
<td align='center'> <xsl:value-of select='.'/> </td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>


<z:MappingData> 
<z:MappingDataElement> 
<z:Department></z:Department> 
<z:LocalCompanyCode>211</z:LocalCompanyCode> 
<z:DecentralizedFunction>DCT</z:DecentralizedFunction> 
<z:LocalCompany>BPX</z:LocalCompany> 
</z:MappingDataElement> 
<z:MappingDataElement> 
<z:Department> </z:Department> 
<z:LocalCompanyCode>284</z:LocalCompanyCode> 
<z:DecentralizedFunction>PRC</z:DecentralizedFunction> 
<z:LocalCompany>BPO</z:LocalCompany> 
</z:MappingDataElement> 
</z:MappingData> 

</xsl:stylesheet>
 
I think what I am basicallu asking is how to use the <xsl:for-each select when the element path I am selecting has a prefix :)
Anyone...?

Roy
 
What you are attempting is normal stuff. The factoid you are probably missing is that document('') (that is, with a zero-length XPath parameter) refers to the stylesheet document itself.

Consider something like:
Code:
select='[COLOR=blue]document('')[/color]/z:MappingData/z:MappingDataElement[1]/*'

This technique is often used for lookup tables, which is reasonably demonstrated here (look near the bottom of the page).

Tom Morrison
 
Hi Tom,

I tried adding the document('') but I still see blank page...

Roy
 
Minor mistake on my part, but the referenced article had it correct.

Code:
<xsl:stylesheet xmlns:xsl='[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform'[/URL]
                version='1.0'
                xmlns:z="MyXMLStuff"
                exclude-result-prefixes="z">
<xsl:output method='html'/>
<xsl:template match="/">
<html>
<body>
<table border='1'>
<tr>
<xsl:for-each select="[COLOR=blue][b]document('')/*[/b][/color]/z:MappingData/z:MappingDataElement[1]/*">
<th bgcolor="#66FF00"><xsl:value-of select="local-name(.)"/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="[COLOR=blue][b]document('')/*[/b][/color]/z:MappingData/z:MappingDataElement">
<tr>
<xsl:for-each select="*">
<td align="center"> <xsl:value-of select="."/> </td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

Tom Morrison
 
Bingo! :)
Now to a different question: I am building this whole file dynamically so at the time when I create it I still don't know it's name so the href='test.xml'?> can't be completed with the file name. Is there any other way to say href to this document without actually specifying it's name (Like the reserved word "this" for an object in Java for example... :))

Thank you in advance!
Roy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top