我正在尝试获取当前元素节点,但我只获取元素的值。请帮帮我..
<root><LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST>
<LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST>
<LIST>
<ROLE>Member</ROLE>
<STATUS>no</STATUS>
</LIST>
<LIST>
<ROLE>Member</ROLE>
<STATUS>no</STATUS>
</LIST>
<LIST>
<ROLE>Member</ROLE>
<STATUS>yes</STATUS>
</LIST>
</root>
我尝试了
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><html>
<h2>
<xsl:element name="test">
<xsl:for-each select="/root/LIST[STATUS='yes']">
<xsl:copy-of select="current()"/>
</xsl:if>
</xsl:for-each>
</xsl:element>
</h2></html>
</xsl:template>
</xsl:stylesheet>
但我只得到元素的值。我需要检索具有值的当前元素。有没有人有想法
答案 0 :(得分:1)
<xsl:element name="test"> <xsl:for-each select="/root/LIST[STATUS='yes']"> <xsl:copy-of select="current()"/> </xsl:for-each> </xsl:element>
这是一种相当古怪的说法:
<test>
<xsl:copy-of select="/root/LIST[STATUS='yes']"/>
</test>
因此,完整转型就是这样:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<h2>
<test>
<xsl:copy-of select="/root/LIST[STATUS='yes']"/>
</test>
</h2>
</html>
</xsl:template>
</xsl:stylesheet>
在提供的XML文档上应用此转换时:
<root>
<LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST>
<LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST>
<LIST>
<ROLE>Member</ROLE>
<STATUS>no</STATUS>
</LIST>
<LIST>
<ROLE>Member</ROLE>
<STATUS>no</STATUS>
</LIST>
<LIST>
<ROLE>Member</ROLE>
<STATUS>yes</STATUS>
</LIST>
</root>
产生了想要的正确结果:
<html>
<h2>
<test>
<LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST>
<LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST>
<LIST>
<ROLE>Member</ROLE>
<STATUS>yes</STATUS>
</LIST>
</test>
</h2>
</html>
答案 1 :(得分:0)
使用...
<xsl:copy-of select="."/>
并删除不匹配的xsl:if like like ...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<h2>
<xsl:element name="test">
<xsl:for-each select="/root/LIST[STATUS='yes']">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:element>
</h2>
</html>
</xsl:template>
</xsl:stylesheet>
结果将是......
<html>
<h2><test><LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST><LIST>
<ROLE>s1</ROLE>
<STATUS>yes</STATUS>
</LIST><LIST>
<ROLE>Member</ROLE>
<STATUS>yes</STATUS>
</LIST>
</test>
</h2>
</html>