简体   繁体   中英

how can I add attribute to a XML CDATA Tags?

XML:

<?xml version="1.0" encoding="UTF-9" ?>
<mailAndMessageSettings>
    <settings>
        <add key="Url" value=""/>
        <add key="UserName" value=""/>
        <add key="Password" value=""/>
    </settings>
    <mail>
        <subject>
            Mp3 Submission
        </subject>
        <body>
            <![CDATA[
                <meta http-equiv="Content-Type" content="text/html; charset="utf-8""/>
                <head></head>

                <body>
                <p>Hi,</p>

                <p>Please find the attached mp3... :-)</p>

                <p><a href="mymp3.mp33">here</a></p>

                <p>Regards,</br>
                Pete</p>

                </body>
                </html> 
            ]]>
        </body>
    </mail>    
</mailAndMessageSettings>

XSLT:

 <xsl:template match="/">
 <xsl:value-of select="/mailAndMessageSettings/mail" disable-output-escaping="yes"/>
 </xsl:template>

Expected output:

 <mail>
            <subject>
                Mp3 Submission
            </subject>
            <body>
                <![CDATA[
                    <meta http-equiv="Content-Type" content="text/html; charset="utf-8""/>
                    <head></head>

                    <body>
                    <p>Hi,</p>

                    <p>Please find the attached mp3... :-)</p>

                    <p><a href="mymp3.mp3" onclick="myfunction();">here</a></p>

                    <p>Regards,</br>
                    Pete</p>

                    </body>
                    </html> 
                ]]>
            </body>
        </mail>    

I want to add an attribute "onclick" on "here" in a CDATA and getting the whole "mail" node? Is it really possible? Can anyone help me with this stuff? Thanks in advance. Your help would be greatly appreciated :)

There are no nodes or tags inside a CDATA section. CDATA means "character data". The only reason for putting stuff inside CDATA is to say "The stuff in here might look like markup, but I don't want it treated as markup; just treat it as text". So if you want to treat it as markup, don't put it in CDATA.

You'll have to resort to string manipulation, like so:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="body[contains(.,'>here&lt;/a>')]">
    <xsl:value-of disable-output-escaping="yes" select="concat(
        '&lt;![CDATA[',
        substring-before(.,'>here&lt;/a>'),
        ' onclick=&quot;myfunction();&quot;>here&lt;/a>',
        substring-after(.,'>here&lt;/a>'),
        ']]>'
      )"/>
  </xsl:template>
</xsl:stylesheet>

But is there a reason why the mail body has to be CDATA in the first place?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM