如何使用XSL检查XML文件中是否存在属性
发布时间:2020-12-16 08:05:28 所属栏目:百科 来源:网络整理
导读:在运行时,我可以有两种格式的XML文件: root diagram graph color= "#ff00ff" xaxis1 2 3 12 312 3123 1231 23 /xaxis yaxis1 2 3 12 312 3123 1231 23 /yaxis /graph /diagram /root root diagram graph xaxis1 2 3 12 312 3123 1231 23 /xaxis yaxis1 2 3
在运行时,我可以有两种格式的XML文件:
> <root> <diagram> <graph color= "#ff00ff"> <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> </graph> </diagram> </root> > <root> <diagram> <graph> <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> </graph> </diagram> </root> 取决于颜色属性的存在 我需要使用XSL。 我试过使用 <xsl: when test="graph[1]/@color"> //some processing here using graph[1]/@color values </xsl:when> 我有错误…
这是使用XSLT模式匹配和“推”“样式的全部功能进行条件处理的一种非常简单的方式,这甚至避免使用条件指令,例如< xsl:if>或< xsl:choose> ;:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/root/diagram[graph[1]/@color]"> Graph[1] has color </xsl:template> <xsl:template match="/root/diagram[not(graph[1]/@color)]"> Graph[1] has not color </xsl:template> </xsl:stylesheet> 当此转换应用于以下XML文档时: <root> <diagram> <graph color= "#ff00ff"> <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> </graph> <graph> <xaxis>101 102 103 1012 10312 103123 101231 1023 </xaxis> <yaxis>101 102 103 1012 10312 103123 101231 1023 </yaxis> </graph> </diagram> </root> 想要的,正确的结果是产生的: Graph[1] has color 当对此XML文档应用相同的转换时: <root> <diagram> <graph> <xaxis>101 102 103 1012 10312 103123 101231 1023 </xaxis> <yaxis>101 102 103 1012 10312 103123 101231 1023 </yaxis> </graph> <graph color= "#ff00ff"> <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> </graph> </diagram> </root> 再次产生想要和正确的结果: Graph[1] has not color 可以自定义此解决方案,并在第一个模板中放置必要的代码,如有必要,可以在第二个模板中。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |