This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

XSLT: decorating a grammatical tree



Here's my problem: imagine an XML document that represents the results of a
grammatical analysis, say of an expression. Only terminals have a value. I'd
like to write an XSL stylesheet that computes the values of other nodes.

Example:
Suppose a very simple grammar for expressions:

	exp ::= add-exp

	add-exp::= mult-exp 
		| add-exp op-add mult-exp 
		| add-exp op-sub mult-exp

	mult-exp ::= primary-exp
		| mult-exp op-mult primary-exp
		| mult-exp op-div primary-exp

	primary-exp ::= litteral
		| par-open exp par-close

Now take "2 + 3" as input; according to the above grammar, it translates to:

	<exp>
	  <add-exp>
	    <add-exp>
	      <mult-exp>
	        <primary-exp>
	          <litteral value="2/>
	        </primary-exp>
	      </mult-exp>
	    </add-exp>
	    <op-add/>
	    <mult-exp>
	      <primary-exp>
	        <litteral value="3"/>
	      </primary-exp>
	    </mult-exp>
	  </add-exp>
	</exp>

I'm trying to write an XSL stylesheet that would translate the above to

	<exp value="5">
	  <add-exp value="5">
	    <add-exp value="2">
	      <mult-exp value="2">
	        <primary-exp value="2">
	          <litteral value="2/>
	        </primary-exp>
	      </mult-exp>
	    </add-exp>
	    <op-add/>
	    <mult-exp>
	      <primary-exp value="3">
	        <litteral value="3"/>
	      </primary-exp>
	    </mult-exp>
	  </add-exp>
	</exp>

I thought of doing something like this:

	<xsl:template match="add-exp">
	  <xsl:choose>
	    <xsl:when test="op-add"> <!-- we're in the case add-exp ::=
add-exp op-add mult-exp -->
	      <xsl:copy>  <!-- copy the current node to result tree -->
	        <xsl:apply-templates/>  <-- copy and process children -->
	        <xsl:attribute name="value"><xsl:value-of
select="number(add-exp/@value) + number(mult-exp/@value)"/></xsl:attribute>
<!-- compute the value -->
	      </xsl:copy>
	    </xsl:when>

	(...)

	</xsl:template>

Of course, this won't work because the expression "number(add-exp/@value) +
number(mult-exp/@value)" applies to the source tree, not the result tree,
and in the source tree non-terminal nodes don't have a value attribute...

So, does anyone have an idea of how I could achieve this transformation with
XSL?

Thanks,
Olivier
ogerardin@cedelglobalservices.com


                                                          
Visit us at http://www.clearstream.com           
                                                          
IMPORTANT MESSAGE

Internet communications are not secure and therefore Clearstream International does not
accept legal responsibility for the contents of this message.

The information contained in this e-mail is confidential and may be legally privileged. It is
intended solely for the addressee. If you are not the intended recipient, any disclosure,
copying, distribution or any action taken or omitted to be taken in reliance on it, is
prohibited and may be unlawful. Any views expressed in this e-mail are those of the
individual sender, except where the sender specifically states them to be the views of
Clearstream International or of any of its affiliates or subsidiaries.

END OF DISCLAIMER


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]