XSLT For-Each不起作用 [英] XSLT For-Each Not Working
问题描述
我有一个xml模式,我想使用XSLT对其进行转换.但是由于某种原因,循环无法正常工作.您能看一下,然后告诉我我可能做错了什么吗?
XML模式:
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-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>
我的XSLT文件如下所示:
And my XSLT file looks like:
<?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>
上面的解决方案在屏幕上打印HTML,但不显示xml.
我已经重写了XSL:
Above solution prints the HTML on screen, but no xml.
I''ve rewritten the XSL:
<?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>
这会将标记中的所有文本合并起来.无需进一步格式化.示例:
Selected.System.ObjectSystem.Management.Automation.PSCustomObjectSystem.ObjectMovie.aviCopied ...
This takes all text in the tags and concatenates it. No further formating is done. Example:
Selected.System.ObjectSystem.Management.Automation.PSCustomObjectSystem.ObjectMovie.aviCopied...
推荐答案
有两个问题
1.对于每个语句,还应提及根元素
There are two problems
1. For each statement should mention the root element also
<xsl:for-each select="Objs/Obj">
2.输入XML格式不正确.不需要XML名称空间声明.更好的XML将是
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">...
我找到了答案.在我的样式表(不是xml,xsl文件)中,我必须添加
I found the answer. In my stylesheet (not the xml, xsl file), I had to add
<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:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
xmlns:ps属性是解释器理解该xml是PowerShell CliXml导出所必需的.如果您不这样做,xsl将不会理解您要尝试执行的操作.奇怪,但显然,它不能仅解释xml.
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.
这篇关于XSLT For-Each不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!