XSLT 和XML 帮助 - 不生成具有两个不同 XML 元素的 html
我在某种情况下将 XSLT 和 XML 结合起来时遇到问题,并且陷入困境,下面是我的问题的示例:
XML:
<a>
<x>
<y testname="test1">
<object elementname="Name" elementvalue="true" elementvalue1="ABC" elementvalue2="ADF">
<comparisonresult>false</comparisonresult>
</object>
</y>
</x>
</a>
XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<font face="Arial">
<h2>HEADINGY</h2>
<h4>Process 1</h4>
<h4>More Process</h4>
<h4>Additional </h4>
<table border="1">
<tr bgcolor="#dccdc">
<th align="center">T1</th>
<th align="center">T2</th>
<th align="center">T3</th>
</tr>
</table>
<h2>Main</h2>
<xsl:for-each select="a/x/y">
<h3>
<xsl:value-of select="@testname" />
</h3>
<h4>Heading 1</h4>
<table border="1" style="display:inline">
<tr bgcolor="#CECEF6">
<th align="center">Item1</th>
<th align="center">Item2</th>
<th align="center">Item3</th>
</tr>
<xsl:for-each select="object">
<tr>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementname" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue1" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue2" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如果您将这两个输入到 http://www.w3schools.com/xml/tryxslt.asp?xmlfile=simple&xsltfile=simple你会得到预期的输出。当我将以下内容添加到 XML 时,问题就出现了,所以它看起来像:
<a>
<x>
<y testname="test1">
<object elementname="Name" elementvalue="true" elementvalue1="ABC" elementvalue2="ADF">
<comparisonresult>false</comparisonresult>
</comparisonobject>
</y>
<x>
<t testname="ComparisonResult">
<step stepname="Add x" stepresult="Add x">
<result>true</result>
</step>
</t>
</a>
和相应的 xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<font face="Arial">
<h2>HEADINGY</h2>
<h4>Process 1</h4>
<h4>More Process</h4>
<xsl:for-each select="a/x/t">
<xsl:for-each select="testname">
<h4>Additional </h4>
<table border="1">
<tr bgcolor="#dccdc">
<th align="center">T1</th>
<th align="center">T2</th>
<th align="center">T3</th>
</tr>
<xsl:for-each select="stepname">
<tr>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@stepname" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@step result" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@result" />
</td>
</table>
<h2>Main</h2>
<xsl:for-each select="a/x/y">
<h3>
<xsl:value-of select="@testname" />
</h3>
<h4>Heading 1</h4>
<table border="1" style="display:inline">
<tr bgcolor="#CECEF6">
<th align="center">Item1</th>
<th align="center">Item2</th>
<th align="center">Item3</th>
</tr>
<xsl:for-each select="object">
<tr>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementname" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue1" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue2" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我只是得到一个空白页面。 我知道这里有很多东西需要消化,但我真的被困住了!
谢谢
I have a problem with combining my XSLT and XML in a certain situation and I am stuck, below is an example of my issue:
XML:
<a>
<x>
<y testname="test1">
<object elementname="Name" elementvalue="true" elementvalue1="ABC" elementvalue2="ADF">
<comparisonresult>false</comparisonresult>
</object>
</y>
</x>
</a>
XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<font face="Arial">
<h2>HEADINGY</h2>
<h4>Process 1</h4>
<h4>More Process</h4>
<h4>Additional </h4>
<table border="1">
<tr bgcolor="#dccdc">
<th align="center">T1</th>
<th align="center">T2</th>
<th align="center">T3</th>
</tr>
</table>
<h2>Main</h2>
<xsl:for-each select="a/x/y">
<h3>
<xsl:value-of select="@testname" />
</h3>
<h4>Heading 1</h4>
<table border="1" style="display:inline">
<tr bgcolor="#CECEF6">
<th align="center">Item1</th>
<th align="center">Item2</th>
<th align="center">Item3</th>
</tr>
<xsl:for-each select="object">
<tr>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementname" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue1" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue2" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If you enter these two into http://www.w3schools.com/xml/tryxslt.asp?xmlfile=simple&xsltfile=simple you get output as expected. The issue comes when i added the following to the XML, so it looks like:
<a>
<x>
<y testname="test1">
<object elementname="Name" elementvalue="true" elementvalue1="ABC" elementvalue2="ADF">
<comparisonresult>false</comparisonresult>
</comparisonobject>
</y>
<x>
<t testname="ComparisonResult">
<step stepname="Add x" stepresult="Add x">
<result>true</result>
</step>
</t>
</a>
and the corresponding xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<font face="Arial">
<h2>HEADINGY</h2>
<h4>Process 1</h4>
<h4>More Process</h4>
<xsl:for-each select="a/x/t">
<xsl:for-each select="testname">
<h4>Additional </h4>
<table border="1">
<tr bgcolor="#dccdc">
<th align="center">T1</th>
<th align="center">T2</th>
<th align="center">T3</th>
</tr>
<xsl:for-each select="stepname">
<tr>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@stepname" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@step result" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@result" />
</td>
</table>
<h2>Main</h2>
<xsl:for-each select="a/x/y">
<h3>
<xsl:value-of select="@testname" />
</h3>
<h4>Heading 1</h4>
<table border="1" style="display:inline">
<tr bgcolor="#CECEF6">
<th align="center">Item1</th>
<th align="center">Item2</th>
<th align="center">Item3</th>
</tr>
<xsl:for-each select="object">
<tr>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementname" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue1" />
</td>
<td bgcolor="#F2F5A9">
<xsl:value-of select="@elementvalue2" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I just get a blank page.
I know there is a lot here to digest, but im really stuck!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
与您的文档结构不匹配 - 它应该是
...但是您的嵌套for-each
还存在进一步的问题。您想要实现的目标最好使用模板来完成。稍后我会添加一个例子。
Your
<xsl:for-each select="a/x/t">
doesn't match your document structure - it should be<xsl:for-each select="a/t">
... but your nestedfor-each
bear further problems.What you are trying to achieve is better done using templates. I'll add an example later.