XSLT使用XPath来找寻XML文档中的信息,这几天在学习XSLT的找寻路径过程中,写了下面这个例子,来加深我对XPath中各个轴的概念的理解
测试用的XML文档和XSLT文档
XML文档:LogReport.xml
<?xml version="1.0" encoding="gb2312"?> <?xml-stylesheet type=‘text/xsl‘ href=‘LogReport.xslt‘?> <LogReport CreateTime="2015/2/7 20:34:17"> <Data>DataA</Data> <Data>DataB</Data> <Data>DataC</Data> <LogList ListName="XXX"> <Log LogLevel="0" LogItem="Zhang" Description="Log1"> <LogMessage Message="abcdefg" /> </Log> <Log LogLevel="0" LogItem="Wang" Description="Log2"> <LogMessage Message="hijklmn" /> </Log> <Log LogLevel="1" LogItem="Lee" Description="Log3"> <LogMessage Message="opqrst" /> </Log> <Log LogLevel="1" LogItem="Zhao" Description="Log4"> <LogMessage Message="uvwxyz" /> </Log> </LogList> <Data>DataX</Data> <Data>DataY</Data> <Data>DataZ</Data> </LogReport>
对应的XSLT文档:LogReport.xslt(框架)
这是LogReport.xslt的骨架,中间<body>标签下的注释部分,是我添加后几节中实验代码的部分
<?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <!--计算Pi值--> <xsl:template match="/"> <html> <head> <meta content="zh-cn" http-equiv="Content-Language"/> <meta content="text/html; charset=utf-16" http-equiv="Content-Type"/> <title>轴</title> </head> <body> <!--这里输入下面要出现的例子--> </body> </html> </xsl:template> </xsl:stylesheet>
1-2)子轴:child,属性轴:attribute
子轴child按文档顺序选中上下文节点所有的直接子节点,如果表达式中没有使用轴区分符,那么将被假定使用了子轴
属性轴选择当前节点的所有属性节点,可以用轴名attribute或预定义的缩写词“ @ ”选择属性
示例代码:
<xsl:text>1.child & 2.attribute</xsl:text> <br/> <xsl:value-of select="/child::LogReport/attribute::CreateTime"/> <br/> <xsl:value-of select="/LogReport/@CreateTime"/>
输出效果:
1.child & 2.attribute
2015/2/7 20:34:17
2015/2/7 20:34:17
3)后代轴:descendant
后代轴选择当前节点的所有后代(子节点、子节点的子节点等),还允许选择属性节点或名称空间节点
示例代码:
<xsl:text>3.descendant</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log"> *<xsl:value-of select="descendant::LogMessage/@Message"/> <br/> </xsl:for-each>
输出效果:
3.descendant
*abcdefg
*hijklmn
*opqrst
*uvwxyz
4)父轴parent
父轴只选择当前节点的直接父节点
示例代码:
<xsl:text>4.parent</xsl:text> <br/> <xsl:value-of select="/LogReport/LogList/Log/parent::node()/@ListName"/> <br/> <xsl:value-of select="/LogReport/LogList/Log/parent::node()/parent::node()/@CreateTime"/>
输出效果:
4.parent
XXX
2015/2/7 20:34:17
5)祖先轴ancestor
祖先轴按文档顺序的相反方向选择当前节点的所有祖先节点
示例代码:
<xsl:text>5.ancestor</xsl:text> <br/> <xsl:value-of select="/LogReport/LogList/Log/ancestor::LogReport/@CreateTime"/> <br/> <xsl:value-of select="/LogReport/LogList/Log/ancestor::LogList/@ListName"/>
输出效果:
5.ancestor
2015/2/7 20:34:17
XXX
6)后继-兄弟轴following-sibling
后继-兄弟轴按照文档顺序选择文档中伺候出现的当前节点的所有兄弟节点
示例代码:
<xsl:text>6.following-sibling</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log[2]/following-sibling::node()"> *<xsl:value-of select="@LogItem"/> <br/> </xsl:for-each>
输出效果:
6.following-sibling
*Lee
*Zhao
7)前驱-兄弟轴:preceding-sibling
前驱-兄弟轴按照与文档顺序相反的方向选择文档中此前出现的当前节点的所有兄弟节点
示例代码:
<xsl:text>7.preceding-sibling</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log[3]/preceding-sibling::node()"> *<xsl:value-of select="@LogItem"/> <br/> </xsl:for-each>
输出效果:
7.preceding-sibling
*Zhang
*Wang
8)后继轴:following
除当前节点的所有后代节点外,后继轴按文档顺序选择文档中当前节点之后出现的所有节点
示例代码:
<xsl:text>8.following</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/following::node()"> *<xsl:value-of select="name()"/>_<xsl:value-of select="."/> <br/> </xsl:for-each>
输出效果:
8.following
*Data_DataX
*_DataX
*Data_DataY
*_DataY
*Data_DataZ
*_DataZ
9)前驱轴:preceding
前驱轴按照与文档顺序相反的方向选择文档中在当前节点之前出现的所有节点
示例代码:
<xsl:text>9.preceding</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/preceding::node()"> *<xsl:value-of select="name()"/>_<xsl:value-of select="."/> <br/> </xsl:for-each>
输出效果:
9.preceding
*xml-stylesheet_type=‘text/xsl‘ href=‘LogReport.xslt‘
*Data_DataA
*_DataA
*Data_DataB
*_DataB
*Data_DataC
*_DataC
10)名称空间轴:namespace
名称空间轴选择当前节点的所有名称空间节点。如果当前节点没有名称空间
示例代码:
<xsl:text>10.namespace</xsl:text> <br/> <xsl:value-of select="LogReport/namespace::node()"/>
输出效果:
10.namespace
http://www.w3.org/XML/1998/namespace
11)自身轴:self
自身轴选择当前节点
示例代码:
<xsl:text>11.self</xsl:text> <br/> <xsl:value-of select="LogReport/self::node()/@CreateTime"/>
输出效果:
11.self
2015/2/7 20:34:17
12)后代或自身轴:descendant-or-self
后代或自身轴按文档顺序选择当前节点及其所有后代节点,该轴不包括属性节点或名称空间节点
示例代码:
<xsl:text>12.descendant-or-self</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/descendant-or-self::node()"> *<xsl:value-of select="name()"/> <br/> </xsl:for-each>
输出效果:
12.descendant-or-self
*LogList
*Log
*LogMessage
*Log
*LogMessage
*Log
*LogMessage
*Log
*LogMessage
13)祖先或自身轴:ancestor-or-self
祖先或自身轴按文档顺序选择当前节点及其所有祖先节点
示例代码:
<xsl:text>13.ancestor-or-self</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log/ancestor-or-self::node()"> *<xsl:value-of select="name()"/>:<xsl:value-of select="node()"/> <br/> </xsl:for-each>
输出效果:
13.ancestor-or-self
*:type=‘text/xsl‘ href=‘LogReport.xslt‘
*LogReport:DataA
*LogList:
*Log:
*Log:
*Log:
*Log:
最终的XSLT文档:LogReport.xslt(全文)
<?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <!--计算Pi值--> <xsl:template match="/"> <html> <head> <meta content="zh-cn" http-equiv="Content-Language"/> <meta content="text/html; charset=utf-16" http-equiv="Content-Type"/> <title>轴</title> </head> <body> <xsl:text>1.child & 2.attribute</xsl:text> <br/> <xsl:value-of select="/child::LogReport/attribute::CreateTime"/> <br/> <xsl:value-of select="/LogReport/@CreateTime"/> <hr/> <xsl:text>3.descendant</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log"> *<xsl:value-of select="descendant::LogMessage/@Message"/> <br/> </xsl:for-each> <hr/> <xsl:text>4.parent</xsl:text> <br/> <xsl:value-of select="/LogReport/LogList/Log/parent::node()/@ListName"/> <br/> <xsl:value-of select="/LogReport/LogList/Log/parent::node()/parent::node()/@CreateTime"/> <hr/> <xsl:text>5.ancestor</xsl:text> <br/> <xsl:value-of select="/LogReport/LogList/Log/ancestor::LogReport/@CreateTime"/> <br/> <xsl:value-of select="/LogReport/LogList/Log/ancestor::LogList/@ListName"/> <hr/> <xsl:text>6.following-sibling</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log[2]/following-sibling::node()"> *<xsl:value-of select="@LogItem"/> <br/> </xsl:for-each> <hr/> <xsl:text>7.preceding-sibling</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log[3]/preceding-sibling::node()"> *<xsl:value-of select="@LogItem"/> <br/> </xsl:for-each> <hr/> <xsl:text>8.following</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/following::node()"> *<xsl:value-of select="name()"/>_<xsl:value-of select="."/> <br/> </xsl:for-each> <hr/> <xsl:text>9.preceding</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/preceding::node()"> *<xsl:value-of select="name()"/>_<xsl:value-of select="."/> <br/> </xsl:for-each> <hr/> <xsl:text>10.namespace</xsl:text> <br/> <xsl:value-of select="LogReport/namespace::node()"/> <hr/> <xsl:text>11.self</xsl:text> <br/> <xsl:value-of select="LogReport/self::node()/@CreateTime"/> <hr/> <xsl:text>12.descendant-or-self</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/descendant-or-self::node()"> *<xsl:value-of select="name()"/> <br/> </xsl:for-each> <hr/> <xsl:text>13.ancestor-or-self</xsl:text> <br/> <xsl:for-each select="/LogReport/LogList/Log/ancestor-or-self::node()"> *<xsl:value-of select="name()"/>:<xsl:value-of select="node()"/> <br/> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
END
原文:http://my.oschina.net/Tsybius2014/blog/376540