|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Text Block within XML using XSLT
I'm attempting to insert a text copy of the original xml document into another xml document I'm creating for use as database input using XSLT. My specific requirements require that I comment out the DOCTYPE first, then copy the rest of the original document into the result xml as one large text block. Following is the XSL I've created to do this.
<xsl:template> <xsl:if test="boolean(true()) = //comment()[contains(.,'DOCTYPE')]" > <xsl:call-template name="comment_doctype" /> <xsl:call-template name="copy_message_body" /> </xsl:if > <xsl:template name="comment_doctype" > <xsl:variable name="d" select="//comment()[contains(.,'DOCTYPE')]" /> <xsl:comment> <xsl:value-of select="$d" /> </xsl:comment> </xsl:template> <xsl:template name="copy_message_body"> <xsl:copy-of select="/root" /> </xsl:template> Here's what I'm trying to accomplish: <a> <b> Text of the original xml including commented DOCTYPE. </b> </a> My first thought was to encompass the <xsl:call-template> calls from the main template with <xsl:text> elements, but I'm coming up with "unexpected child" errors when I'm testing the XLST. If anyone has any suggestions on how to accomplish this, they would be greatly appreciated. And if I'm making no sense at all, someone also please bring that to my attention. Thanks again. |
|
#2
|
||||
|
||||
|
Solution
Try the following. My input XML that I used for testing did not have a !DOCTYPE in it. I don't see why the following code would include it in its output. Let me know whether or not this is a suitable solution.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<a>
<b>
<!-- the input XML will be printed here -->
<xsl:apply-templates select="child::node()"/>
</b>
</a>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="comment()|processing-instruction()">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>
|
|
#3
|
|||
|
|||
|
Hey Matt. Thanks for looking into this for me. I'm actually wanting to put the embedded XML document into a database and want to see the entire document "as is". The complication is that my parser (in-house) won't recognize comment fields and will strip them out. That's why I'm attempting to copy the original document as a text field (to trick the parser and have it not parse the text xml subset). I'm looking for something like a CDATA block, but in just general text. Or, xml that is completely escaped out.
What do you think? Is this doable? I have my doubts. |
|
#4
|
|||
|
|||
|
Use Namespaces Possibly?
I'm not too familiar with using namespaces, but I'm wondering if this would make a fitting time to learn. If I put the needed xml block into it's own namespace, maybe I could treat that block differently within my java application?
Any thoughts? |
|
#5
|
||||
|
||||
|
I believe that even with different namespaces that your parser will still remove comments from the XML block.
I am curious to know what the value of the <b> element is when read into your Java program after the XSL transformation if you modify the above template to the following (depending on how your parser treats the CDATA section the XML in the b element may be preserved or the < and > may be converted to < and >): Code:
<xsl:template match="/"> <xsl:variable name="START_CDATA" select="'<![CDATA['"/> <xsl:variable name="END_CDATA" select="']]>'"/> <a> <b> <xsl:value-of disable-output-escaping="yes" select="$START_CDATA"/> <xsl:apply-templates select="child::node()"/> <xsl:value-of disable-output-escaping="yes" select="$END_CDATA"/> </b> </a> </xsl:template> If that does not work, well, I am out of ideas. |
|
#6
|
|||
|
|||
|
Thanks again. I think the CDATA section is where I should have been looking all along. Your suggestion is going to work perfectly. Now I'm just faced with the possibility for nested CDATA sections which I've read is not a good thing. It looks like more work for me to keep plugging through this thing.
|
|
#7
|
||||
|
||||
|
> Now I'm just faced with the possibility for nested CDATA sections which I've read is not a good thing.
Nested CDATA sections are against the spec. If an XML parser does not error out (or provide some other unwanted result) then it is in violation. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Text Block within XML using XSLT |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|