简体   繁体   English

如何将属性添加到XML CDATA标签?

[英]how can I add attribute to a XML CDATA Tags?

XML: 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: 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? 我想在CDATA中的“这里”上添加属性“ onclick”并获取整个“邮件”节点吗? 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节内没有节点或标签。 CDATA means "character data". CDATA的意思是“字符数据”。 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". 将内容放入CDATA的唯一原因是说“此处的内容可能看起来像标记,但我不希望将其视为标记;仅将其视为文本”。 So if you want to treat it as markup, don't put it in CDATA. 因此,如果您要将其视为标记,请不要将其放入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? 但是,为什么邮件正文必须首先是CDATA是有原因的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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