[SlugBug] a perl/xslt question. potentially inappropriate.

Chris J cej at nightwolf.org.uk
Sat Apr 16 12:56:05 BST 2005


And Lo! The Great Prophet Mike Dewar uttered these words of wisdom:
> 
> Here's the XML (from the Scilab Source)
> 
> <P>
>      For a vector or a matrix <VERB>x</VERB>, <VERB>y=sum(x)</VERB>  
> returns in the scalar <VERB>y</VERB> the sum of all the entries
>      of <VERB>x</VERB>.</P>
> 
> and here's what i want it to look like for the wiki:
> 
> <P>
>      For a vector or a matrix '''x''', '''y=sum(x)'''  returns in the 
> scalar '''y''' the sum of all the entries
>      of '''x'''.</P>
> 
> Can anyone help? I think the problem I'm having is that the VERB things 
> are nested in the text of the P tags. I can only seem to get the all 
> the text() elements of P then the text inside the VERB tags. I cant 
> keep them nested...

Does whitespace matter? If not, then running that fragment as an XML 
document in its own right through this stylesheet with Sablotron gives the 
results you're looking for...

<?xml version="1.0"?>

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

        <xsl:template match="/">
                <xsl:apply-templates />
        </xsl:template>

        <xsl:template match="P">
                <P><xsl:apply-templates /></P>
        </xsl:template>

        <xsl:template match="VERB">
                '''<xsl:apply-templates />'''
        </xsl:template>

</xsl:transform>

What this does is match the root node first (as an entry point obviously), 
then recursively processes all XML nodes it comes across. When it comes 
across <P>, it runs the template for match="P", which essentially copies 
the  input to the output, wrapped in <P>. Note though I'm doing this with 
apply-templates again, so we get recursive processing of nodes. This means 
that VERB gets picked up and processed by the appropriate template...

This spits out:
<P>
        For a vector or a matrix 
                '''x'''
        , 
                '''y=sum(x)'''
          
        returns in the scalar 
                '''y'''
         the sum of all the entries
        of 
                '''x'''
        .</P>

See why I asked if whitespace matters?. If this is simply going to produce 
another XML document, or a HTML document, then the whitespace shouldn't 
matter...

What you need to remember is that text strings are nodes in their own 
right. Nodes can be either textual, an element, or an attribute. The 
fragment above could be represented in a tree:

Root Node (/)
   \---	element P
	|-- #text = "For a vector..."
	|-- element VERB
	|	\--- #text = "x"
	|-- #text = ","
	|-- element VERB
	|	\--- #text = "y = sum(x)"
	|-- #text = "returns in the ..."
		.
		.
		.

and so on. Hopefully this makes it a little clearer? Shout if not :-)

Chris...

-- 
\ Chris Johnson                 \ NP: 
 \ cej at nightwolf.org.uk          \  
  \ http://cej.nightwolf.org.uk/  \ 
   \ http://redclaw.org.uk/        ~---------------------------------------




More information about the SlugBug mailing list