Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XSL检查XML文件中是否存在属性_Xml_Xslt_Xpath - Fatal编程技术网

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

如何使用XSL检查XML文件中是否存在属性,xml,xslt,xpath,Xml,Xslt,Xpath,在运行时,我可以有两种格式的XML文件: 1 2 3 12 312 3123 1231 23 1 2 3 12 312 3123 1231 23 1 2 3 12 312 3123 1231 23 1 2 3 12 312 3123 1231 23 取决于颜色属性的存在 我必须处理xaxis和yaxis的值 我需要使用XSL来实现这一点。 有谁能帮我提示一个片段,我可以检查这些条件吗 我试着用 <xsl: when test="graph[1]/@color">

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

  • 
    1 2 3 12 312 3123 1231 23 
    1 2 3 12 312 3123 1231 23 
    
  • 
    1 2 3 12 312 3123 1231 23 
    1 2 3 12 312 3123 1231 23 
    
  • 取决于颜色属性的存在 我必须处理xaxis和yaxis的值

    我需要使用XSL来实现这一点。 有谁能帮我提示一个片段,我可以检查这些条件吗

    我试着用

    <xsl: when test="graph[1]/@color">
         //some processing here using graph[1]/@color values
    </xsl:when>
    
    
    //这里使用图形[1]/@颜色值进行一些处理
    

    我有一个错误…

    我不明白-除了对使用应用模板进行一些轻微的语法调整之外:

    <xsl:template match="graph[1][@color]">
      <!-- your processing here -->
    </xsl:template>
    
    
    
    在不知道您真正想要做什么的情况下,我们无法告诉您多少。

    
    
    <xsl:when test="graph[1]/@color">
         //some processing here using graph[1]/@color values
    </xsl:when>
    
    //这里使用图形[1]/@颜色值进行一些处理

    我想在这里做一个猜测,因为您的问题缺少很多重要信息,例如上下文,在上下文中,使用XSLT模式匹配的全部功能和专门的“推送”样式执行条件处理是一种非常简单的方法,这甚至可以避免使用条件指令,如

    <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
    
    <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
    
    用户可以自定义此解决方案并将所需的任何代码放入第一个模板中,必要时放入第二个模板中。

    像这样在一个匹配中自定义模板
    
    完成任务
    完成任务
    **
    
    您是否使用了
    xsl:choose
    ::(
    xsl:when
    +|
    xsl:others
    )指令?是的,我使用了xsl:choose…xsl:when+xsl:others条件无效..上下文节点是什么<代码>/文档根,
    元素,
    元素,
    元素…?好问题,+1。请参阅我的答案,以获得一个简单的解决方案,该解决方案最大限度地使用了XSLT模式匹配和推式处理等强大功能我只想根据xml文件中属性“color”的存在来处理图形……就像(属性是否存在?)处理1;过程2;此外,此图[1][@color]无法识别属性presence是的。如果
    graph
    元素上没有
    color
    属性,则不会选择该模板。是的,这就是重点。该XML文件中可能有或没有“color”属性,XSL必须检查是否有。是。例如,基于属性“color”的存在,我需要设置一些变量,比如..graph color。现在我想要一个简单的if-else语句,它基于属性是否存在。如果为true,则我可以设置属性中包含的变量。否则,我可以使用默认颜色。请编辑您的帖子,以包含更多样式表,以便我们了解您试图实现的目标。@Dimitre Novatchev是否可以将此解决方案用于说明中?我正在使用XSLT从XML生成代码,我想筛选一些getter和setter?@chepseskaf:如果您的意思是可以在
    xsl:apply templates
    select
    属性中使用谓词,而不是在匹配模式中使用它们,那么答案是肯定的,但这样做是没有必要的,并且会导致更大的错误“推“或命令式的处理方式,这降低了简单性、可理解性、可维护性和优化性。很遗憾看到这个有趣的问题,虽然已经10年了,但没有选择答案。但我认为OP想要的是
    ,以处理所有图形节点。@Fuujuhi,也许你是对的,但在SO环境中“所有猜测都是相等的”。这里OP没有透露他的目的——只是他在条件处理方面有问题。这个答案显示了这个问题的一个很好的解决方案,而不需要尝试更深的猜测。
    <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
    
    <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>**