Click here to Skip to main content
15,944,832 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I have an xml schema and I want to transform it using XSLT. But for some reason the loop isn't working. Could you look at it and tell me what I might be doing wrong?

XML schema:
XML
<?xml-stylesheet type="text/xsl" href="ShowLog.xsl"?>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>Selected.System.Object</T>
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="FileName">Movie.avi</S>
      <S N="Status">Copied</S>
    </MS>
  </Obj>
  .
  .
  .
</Objs>


And my XSLT file looks like:
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<head><title>Transformation Log</title></head>
			<body>
				<h1>Transformation Log</h1>
				<table border="1">
					<tr>
						<th>File Name</th>
					</tr>
					<xsl:for-each select="//Obj">
					<tr>
						<td><xsl:value-of select="MS/S[@N='FileName']" /></td>
					</tr>
					</xsl:for-each>
				</table>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Above solution prints the HTML on screen, but no xml.

I've rewritten the XSL:
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<head><title>Transformation Log</title></head>
			<body>
				<h1>Transformation Log</h1>
				<table border="1">
					<tr>
						<th>File Name</th>
						<th>From</th>
						<th>To</th>
						<th>Status</th>
					</tr>
					<xsl:apply-templates />
				</table>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="Obj">
		<xsl:apply-templates select="MS" />
	</xsl:template>
	
	<xsl:template match="MS">
			<tr>
				<xsl:appy-templates select="S" />
			</tr>
	</xsl:template>
	
	<xsl:template match="S">
		<td><xsl:value-of select="." /></td>
	</xsl:template>
</xsl:stylesheet>

This takes all text in the tags and concatenates it. No further formating is done. Example:
Selected.System.ObjectSystem.Management.Automation.PSCustomObjectSystem.ObjectMovie.aviCopied...
Posted
Updated 20-Sep-12 23:36pm
v4

There are two problems

1. For each statement should mention the root element also
<xsl:for-each select="Objs/Obj">


2. Input XML is not well formed. XML namespace declaration is not required for it. Better XML will be
<?xml version="1.0" encoding="ISO-8859-1"?>
<Objs Version="1.1.0.1">
  <Obj RefId="0">...
 
Share this answer
 
v2
Comments
KenBonny 21-Sep-12 5:20am    
Tried both things, they both don't work.

Btw: "//Obj" should get all the Obj tags in the document. But none of it works. :(

The schema is there because this is an output of PowerShell's Export-CliXml cmdlet.
I found the answer. In my stylesheet (not the xml, xsl file), I had to add
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ps="http://schemas.microsoft.com/powershell/2004/04">
</xsl:stylesheet>


Original:
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>


The xmlns:ps attribute is necessary for the interpreter to understand that this xml is a PowerShell CliXml export. If you don't do this, xsl won't understand what you are trying to do. It's odd, but apparently, it can't just interpret the xml.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900