开发者

Fixing indentation of CDATA sections

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-elemen开发者_JAVA百科ts="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>


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.


This is kludgy but it should work and is super quick. Simply use <xsl:text>:

<xsl:copy />
<xsl:text>
</xsl:text>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜