如何使用 XSL 检查 XML 文件中是否存在属性 [英] How to check if an attribute exists in a XML file using XSL

查看:38
本文介绍了如何使用 XSL 检查 XML 文件中是否存在属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时我可以有两种格式的 XML 文件:

At runtime I can have two formats of a XML file:

<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>
    

  • 取决于是否存在颜色属性我必须处理 xaxis 和 yaxis 的值.

    Depending on the presence of the color attribute i have to process the values of the xaxis and yaxis.

    我需要使用 XSL 来做到这一点.任何人都可以帮助我提示我可以检查这些条件的片段.

    I need to do this using XSL. Can anyone help me in hinting me a snippet where i can check these condtions.

    我尝试使用

    <xsl: when test="graph[1]/@color">
         //some processing here using graph[1]/@color values
    </xsl:when>
    

    我有一个错误...

    推荐答案

    这里有一种非常简单的方法来进行条件处理,使用 XSLT 模式匹配的全部功能和专门的推送"样式,以及这甚至避免了使用条件指令的需要,例如 :

    Here is a very simple way to do conditional processing using the full power of XSLT pattern matching and exclusively "push" style, and this even avoids the need to use conditional instructions such as <xsl:if> or <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
    

    您可以自定义此解决方案并将任何必要的代码放入第一个模板中,如有必要,放入第二个模板中.

    One can customize this solution and put whatever code is necessary inside the first template and if necessary, inside the second template.

    这篇关于如何使用 XSL 检查 XML 文件中是否存在属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆