Jump to content

Condition check for XML attributes using XSL


srivatsahg

Recommended Posts

Hello 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>Possibility 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 ...Can anyone help me in realizing this condition check

Link to comment
Share on other sites

You can simply chain another predicate, like:

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

When you use a predicate, you're still processing the same node you were processing before it. What you had was actually advancing to the attribute node itself.If for whatever reason chaining a predicate is not feasable, you can also use "../" to back up one level (i.e. the element with the color attribute itself), but that will have to be prefixed on every XPath statements that targets that element.

Link to comment
Share on other sites

Thanks a lot !!That works...

You can simply chain another predicate, like:
<xsl: when test="graph[1][@color]">//some processing here using graph[1] has a "color" attribute</xsl:when>

When you use a predicate, you're still processing the same node you were processing before it. What you had was actually advancing to the attribute node itself.If for whatever reason chaining a predicate is not feasable, you can also use "../" to back up one level (i.e. the element with the color attribute itself), but that will have to be prefixed on every XPath statements that targets that element.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...