需要读取前一个节点的子值以使用 xsl 格式化当前节点的值?
我需要在行数据标记中显示行数据的内容时使用格式标记。
xml如下:“temp.xml”
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Temp.xsl"?>
<ALL_DATA>
<TITLES>
<VALUE1>Title1</VALUE1>
<VALUE2>Title2</VALUE2>
<FVALUE1>Title3</FVALUE1>
</TITLES>
<FORMATS>
<VALUE1>I5</VALUE1>
<VALUE2>I3</VALUE2>
<FVALUE1>F1.1</FVALUE1>
</FORMATS>
<MY_DATA>
<ROWDATA>
<VALUE1>5</VALUE1>
<VALUE2>33</VALUE2>
<FVALUE1>2.11</FVALUE1>
</ROWDATA>
<ROWDATA>
<VALUE1>34</VALUE1>
<VALUE2>12</VALUE2>
<FVALUE1>239.81</FVALUE1>
</ROWDATA>
</MY_DATA>
</ALL_DATA>
,我尝试的xsl是:“temp.xsl”
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template name="MY_TEMPLATE" match="/">
<html>
<body>
<table border="1">
<TR border="1">
<xsl:for-each select="/ALL_DATA/TITLES/*">
<th border="1">
<xsl:value-of select="."/>
</th>
</xsl:for-each>
</TR>
<xsl:for-each select="/ALL_DATA/MY_DATA/ROWDATA">
<TR>
<xsl:for-each select="*">
<TD width ="130">
<xsl:value-of select="."/>:-:
<xsl:variable name="cur_node_name" select="name(.)"/>
<xsl:for-each select="/ALL_DATA/FORMATS[name(.)]">
<!--<xsl:template match="$cur_node_name"> -->
<xsl:value-of select="."/>
<!--</xsl:template>-->
</xsl:for-each>
</TD>
</xsl:for-each>
</TR>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
很抱歉发布整个内容。 我觉得我可以得到一些帮助。在上面的xml中,我想在显示Mydata/rowdata/value1时使用formats/value1。 上面的 Xsl 迭代标题块并在 中显示标题。然后第二个 for-each 块将迭代 rowdata 及其子项。 在显示子项时,我需要使用格式标签来显示行数据的子项。
上述 xsl 给出的第三个标签 FValue1 的输出为: "2.11:-: I5 I3 F1.1"
;但我的期望是:“2.11:-: F1.1”
。如果我得到这个解决方案,我就可以做剩下的事情。我知道只需修改内部 for 循环,这是可能的。但无法得到这一点,
如果有人有建议,请帮忙。 谢谢。
I need to use the formats tag while displaying the contents of rowdata, in the rowdata tags.
the xml is follows: "temp.xml"
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Temp.xsl"?>
<ALL_DATA>
<TITLES>
<VALUE1>Title1</VALUE1>
<VALUE2>Title2</VALUE2>
<FVALUE1>Title3</FVALUE1>
</TITLES>
<FORMATS>
<VALUE1>I5</VALUE1>
<VALUE2>I3</VALUE2>
<FVALUE1>F1.1</FVALUE1>
</FORMATS>
<MY_DATA>
<ROWDATA>
<VALUE1>5</VALUE1>
<VALUE2>33</VALUE2>
<FVALUE1>2.11</FVALUE1>
</ROWDATA>
<ROWDATA>
<VALUE1>34</VALUE1>
<VALUE2>12</VALUE2>
<FVALUE1>239.81</FVALUE1>
</ROWDATA>
</MY_DATA>
</ALL_DATA>
and i tried xsl is: "temp.xsl"
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template name="MY_TEMPLATE" match="/">
<html>
<body>
<table border="1">
<TR border="1">
<xsl:for-each select="/ALL_DATA/TITLES/*">
<th border="1">
<xsl:value-of select="."/>
</th>
</xsl:for-each>
</TR>
<xsl:for-each select="/ALL_DATA/MY_DATA/ROWDATA">
<TR>
<xsl:for-each select="*">
<TD width ="130">
<xsl:value-of select="."/>:-:
<xsl:variable name="cur_node_name" select="name(.)"/>
<xsl:for-each select="/ALL_DATA/FORMATS[name(.)]">
<!--<xsl:template match="$cur_node_name"> -->
<xsl:value-of select="."/>
<!--</xsl:template>-->
</xsl:for-each>
</TD>
</xsl:for-each>
</TR>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I am sorry to post entire content.
I feel i can get some help. In above xml, i want to use formats/value1 while displaying the Mydata/rowdata/value1.
The above Xsl iterates the titles block and display the titles in <th>
. and then the second for-each block will iterates through rowdata, and its child items.
In displaying child items, i need to use formats tags to display rowdata's childs.
The above xsl is giving output for 3rd tag FValue1 as : "2.11:-: I5 I3 F1.1"
; But my expectation is : "2.11:-: F1.1"
. If i get this solution, i can do the rest. I know just by modifying inner for loop, that can be possible. but could not get that
Please help if any one have suggestion how to.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的样本数据没有过度简化,您的 xslt 可能会完全删除
for-each
,如下所示:If you sample data isn't oversimplified your xslt could drop the
for-each
altogether and look like this: