markslade Posted August 8, 2007 Share Posted August 8, 2007 Hey all. I'm new to XSLT and am working on a play website that uses XML/XSLT to render everything. My question is simple... is there a way in XSLT not to translate the contents of certain elements? For example... <page> <title>Yada yada</title> <content> <section title='Some heading'> <rawHTML> <p>This is an image link <a href='/somewhere.html'><img src='/someimage.jpg'/></a></p> </rawHTML> </section> </content></page> In the case above, is there a way for me to tell XSLT to leave all children of rawHTML as they are? I find that if I do this... <xsl:template match='rawHTML'> <xsl:apply-templates /></xsl:template> ...without any templates matching the children of rawHTML, then the XSLT engine just outputs the XML with the tags stripped. I end up with text, no paragraph, no link, no image. I'd like to avoid having to have a XSLT file full of translations from one thing into the same thing. Right now I'm working around it with code like this: <xsl:template match='img'> <img src='@url' alt='@alt'/></xsl:template> ..but that seems horribly inefficient. Any ideas?Mark Link to comment Share on other sites More sharing options...
boen_robot Posted August 8, 2007 Share Posted August 8, 2007 Well, there are two ways. One is to match all descendatns of whatever element you want to preserve and do a shallow copy (useful if you need to change some element in the preserved part), or just make a complete copy.You could get away with the later like so: <xsl:template match='rawHTML'> <xsl:copy-of select="text()|*"/></xsl:template> And for other cases, you might need to do: <xsl:template match='rawHTML//*'> <xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> I'm not completely sure of the effects of that one though. Putting it into a good use takes some trial&error. Link to comment Share on other sites More sharing options...
markslade Posted August 8, 2007 Author Share Posted August 8, 2007 Well, there are two ways. One is to match all descendatns of whatever element you want to preserve and do a shallow copy (useful if you need to change some element in the preserved part), or just make a complete copy.You could get away with the later like so:<xsl:template match='rawHTML'> <xsl:copy-of select="text()|*"/></xsl:template> And for other cases, you might need to do: <xsl:template match='rawHTML//*'> <xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> I'm not completely sure of the effects of that one though. Putting it into a good use takes some trial&error. Your first solution works beautifully. Thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.