Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

xpath

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

  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.

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

I tried using

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

i got an error ...

like image 680
srivatsa Avatar asked Nov 10 '10 16:11

srivatsa


People also ask

How do you reference xsl in XML?

Correct Style Sheet Declaration The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0".

Which xsl:element is used to extract information from XML document?

XSLT <xsl:value-of> Element.

How does xsl work with XML?

XSLT Processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a formatted document in the form of XML, HTML, or text format. This formatted document is then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-user.


1 Answers

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> 

when this transformation is applied on the following XML document:

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

<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 solution and put whatever code is necessary inside the first template and if necessary, inside the second template.

like image 61
Dimitre Novatchev Avatar answered Sep 26 '22 06:09

Dimitre Novatchev