How to manipulate xml tag in xslt which contains xml tag along with its attributes as its attribute?

362 views Asked by At

My XML looks like this.

<PlayBack>
<![CDATA[<Info Event="WindowActivate" ControlName="" ControlType="" ControlRectSC="" ControlRectCL="" ParentName="" ParentType="" ControlData="" ControlMouseCL="" DialogName="" ExeName=""  MouseButton="" />]]>
</PlayBack>

I want to fetch the Info tag attributes values and store them in some variables.

result expected

<xsl:variable name="Event">
      "WindowActivate"    //here i need this value
</xsl:variable>
2

There are 2 answers

2
Mike On

In the example you've given, there is no Info tag - just a string of text which just happens to look to human eyes as if it contained an info tag. That's what CData is - a way of storing text without interpreting it as tags.

If you want to use it, you need to do 2 passes. The first extracts the data and writes it out to an intermediate file using xsl:value-of (not copy-of - you need to get rid of the CDATA tag). The second reads in the result and can then use the tags.

2
Martin Honnen On

You can download an XSLT 3.0 processor like Saxon 9.6 HE from http://saxon.sourceforge.net/ and then use the parse-xml function introduced in XPath 3.0:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="PlayBack">
  <xsl:variable name="info" select="parse-xml(.)/Info"/>
  <xsl:variable name="event" select="$info/@Event"/>
  <xsl:value-of select="$event"/>
</xsl:template>

</xsl:stylesheet>

If you need to use an XSLT 1.0 or 2.0 processor then you need to check whether it supports or allows you to implement a similar extension function, if not, you need to use the already suggested two step solution of transforming in a first step to a new XML document (<xsl:template match="PlayBack"><xsl:copy><xsl:value-of select="." disable-output-escaping="yes"/></xsl:copy></xsl:template>) and then process that file with a second stylesheet.