Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4055540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:42:50+00:00 2026-05-20T14:42:50+00:00

Im completely new to XML/XSL/XSLT, and while i have been digging msdn, 3schools.com and

  • 0

Im completely new to XML/XSL/XSLT, and while i have been digging msdn, 3schools.com and google for the past hour i cant figure this one out. I think is because CDATA isnt parsed by xml, but I thought that as my edit did work on the node i should be able to fix this…

Note that this is not a very important issue, I just want to learn a little bit more of XSL and what better way than fixing stuff that doesnt seem to work as i want it to.

So… my script saves options on an XML file in which i will also save some code snippets which might contain characters that need to be escaped. A small example would be:

<Snippet title="Version Test">
<![CDATA[
version := "AHK Version: " a_ahkversion
unicode := "& Supports Unicode: " (a_isunicode ? "Yes" : "No")
Msgbox % version "`n" unicode
]]>
</Snippet>

With the following xsl i get a fairly good indentation:

<!-- Extracted from: http://www.dpawson.co.uk/xsl/sect2/pretty.html (v2) -->
<!-- Cdata info: http://www.altova.com/forum/default.aspx?g=posts&t=1000002342 -->
<!-- Modified By RaptorX -->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" 
            indent="yes" 
            encoding="UTF-8"
            cdata-section-elements="Snippet"/>

<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>
<!-- I have to keep the indentation here in this file as 
i want it to be on the XML file -->

Well, basically it doesnt match CDATA sections, so googling around I found that i could do the following which helps a little but produces this output:

xsl:copy-of select="@*|node()" /> << -- by adding that i match cdata nodes too

Output:
<Snippet title="Version Test">
   <![CDATA[
version := "AHK Version: " a_ahkversion
unicode := "Supports Unicode: " (a_isunicode ? "Yes" : "No")
Msgbox % version "`n" unicode
   ]]></Snippet>   <<-- here is the problem I cant seem to put a newline there lol

So the question is:

how do i tell xsl to indent the CDATA section as it does with everything else:

    <root>
        <child/>
    </root>

   <Snippet title="Version Test">
   <![CDATA[
    version := "AHK Version: " a_ahkversion
    unicode := "Supports Unicode: " (a_isunicode ? "Yes" : "No")
    Msgbox % version "`n" unicode
   ]]> << --- this is what im looking for
   </Snippet>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:42:51+00:00Added an answer on May 20, 2026 at 2:42 pm

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"
      cdata-section-elements="Snippet"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="Snippet/text()">
      <xsl:call-template name="replace"/>
      <xsl:text>&#xA;</xsl:text>
     </xsl:template>
    
     <xsl:template name="replace">
      <xsl:param name="pText" select="."/>
    
      <xsl:if test="string-length($pText) >0">
       <xsl:choose>
        <xsl:when test="not(contains($pText,'&#xA;'))">
         <xsl:value-of select="$pText"/>
        </xsl:when>
    
        <xsl:otherwise>
         <xsl:value-of select=
            "substring-before($pText, '&#xA;')"/>
         <xsl:text>&#xA;&#9;&#9;</xsl:text>
    
         <xsl:call-template name="replace">
          <xsl:with-param name="pText"
           select="substring-after($pText, '&#xA;')"/>
         </xsl:call-template>
        </xsl:otherwise>
       </xsl:choose>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <Snippet title="Version Test">
    <![CDATA[
    version := "AHK Version: " a_ahkversion
    unicode := "& Supports Unicode: " (a_isunicode ? "Yes" : "No")
    Msgbox % version "`n" unicode]]>
    </Snippet>
    

    produces the wanted, correct result:

    <Snippet title="Version Test"><![CDATA[
    
            version := "AHK Version: " a_ahkversion
            unicode := "& Supports Unicode: " (a_isunicode ? "Yes" : "No")
            Msgbox % version "`n" unicode
    
    ]]></Snippet>
    

    Explanation:

    1. The identity rule copies every node “as-is”.

    2. The cdata-section-elements="Snippet" attribute of <xsl:output> instructs the XSLT processor to serialize any text node of any Snippet element as a CDATA section.

    3. There is a single template that overrides the identity template — when matching a text node that is a child of a Snippet element.

    4. The processing of any such text-node is done by calling the replace template, which replaces any NL character with NL followed by two tab characters. When these replacements are done, one last NL character is output, so that the end tag </Snippet> will be on a new line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Completely new to using simple XML library in PHP, and have been using the
Completely new to java and I have been playing around with regex in a
I am trying to sort an xml using xsl. Got some sample from xml.com.
Hopefully this will be a simple fix. I'm completely new to XSL but I've
I have an XML file that references an XSL file (like ya do) that
I'm completely new to XML Schema, XML Stylesheets and XQuery. To me, XML is
I am COMPLETELY NEW to xml... I was wondering, what is the best guide
I have a client with an existing Typo3 site but I'm completely new to
I have worked with ASP.NET(C#, classic/MVC), Django(Python) and CI(php) but I am completely new
I'm completely new to Oracle's XDB, in particular using it to generate XML output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.