如何使用 XSL 检查 XML 文件中是否存在属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4146648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:23:26  来源:igfitidea点击:

How to check if an attribute exists in a XML file using XSL

xmlxsltxpath

提问by srivatsa

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

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

  1. <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>
    
  2. <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>
    
  1. <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>
    
  2. <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>
    

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

根据颜色属性的存在,我必须处理 xaxis 和 yaxis 的值。

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

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

I tried using

我尝试使用

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

i got an error ...

我有一个错误...

回答by Dimitre Novatchev

Here is a very simple way to do conditional processingusing 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>:

这是使用 XSLT 模式匹配的全部功能和专门的“推送”样式进行条件处理的一种非常简单的方法,这甚至避免了使用条件指令的需要,例如<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>

when this transformation is applied on the following XML document:

当此转换应用于以下 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>

the wanted, correct result is produced:

产生了想要的、正确的结果

  Graph[1] has color

when the same transformation is applied on this XML document:

当对这个 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>

again the wanted and correct result is produced:

再次产生了想要的和正确的结果

  Graph[1] has not color

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

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

回答by vignesh

Customize the template in one match like this

像这样在一场比赛中自定义模板

<xsl:template match="diagram/graph">
  <xsl:choose>
    <xsl:when test="@color">
         Do the Task
    </xsl:when>
    <xsl:otherwise>
         Do the Task
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>**

回答by Jim Garrison

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

I'm going to make a guess here since your question is missing a lot of important information, such as the context in which thie <xsl:when...appears. If your comment is correct, what you want to do is process graph[1]/xaxisand .../yaxis, not graph[1]/@colorvalues.

我将在这里进行猜测,因为您的问题缺少许多重要信息,例如 thie<xsl:when...出现的上下文。如果您的评论是正确的,那么您想要做的是处理graph[1]/xaxis.../yaxis,而不是graph[1]/@color值。

回答by annakata

I don't get it - apart from some slight syntax tweak towards using apply-templates:

我不明白 - 除了使用 apply-templates 的一些轻微的语法调整:

<xsl:template match="graph[1][@color]">
  <!-- your processing here -->
</xsl:template>

There's not much we can tell you without knowing what you actually want to do.

在不知道您真正想做什么的情况下,我们无法告诉您太多。