Convert an xml element whose content is inside CDATA

前端 未结 3 1242
轮回少年
轮回少年 2020-12-01 21:56

I have a xml fragment like below


    welcome to my page
相关标签:
3条回答
  • 2020-12-01 22:31

    You can't "remove" the CDATA, but you can achieve the desired output somewhat crudely:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
       <Detail>
            <xsl:variable name="before" select="substring-before(//Detail,'&lt;div class=&quot;heading&quot;&gt;')" />
            <xsl:variable name="afteropen" select="substring-after(//Detail,'&lt;div class=&quot;heading&quot;&gt;')" />
            <xsl:variable name="body" select="substring-before($afteropen, '&lt;/div&gt;')" />
            <xsl:variable name="after" select="substring-after($afteropen, '&lt;/div&gt;')" />
            <xsl:value-of select="concat($before, '&lt;h1&gt;', $body, '&lt;/h1&gt;',$after)"
                    disable-output-escaping="yes"       />
       </Detail>
    </xsl:template>
    </xsl:stylesheet>
    

    This will work for the first type of div you're trying to parse and you can follow something similar with the second one. It could be made more generic with some effort.

    0 讨论(0)
  • 2020-12-01 22:46

    What about running two transforms.

    Pass 1.)

    <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet
       version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    
      <xsl:template match="/">
        <xsl:apply-templates />
      </xsl:template>
    
        <xsl:template match="Detail">
            <Detail>
                <xsl:copy-of select="@*"/>
            <xsl:value-of select="." disable-output-escaping="yes" />
            </Detail>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Will produce:

    <?xml version="1.0" encoding="UTF-8"?>
    <Detail uid="6"> 
        <div class="heading">welcome to my page</div>
        <div class="paragraph">this is paraph</div>
    </Detail>
    

    Pass 2.)

    <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet
       version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    
      <xsl:template match="/">
        <xsl:apply-templates />
      </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*| node()" />
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="div[@class='heading']">
            <h1><xsl:value-of select="."/></h1>
        </xsl:template>
    
        <xsl:template match="div[@class='paragraph']">
            <p><xsl:value-of select="."/></p>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Produces:

    <?xml version="1.0" encoding="UTF-8"?>
    <Detail uid="6">
    <h1>welcome to my page</h1>
    <p>this is paraph</p>
    </Detail>
    
    0 讨论(0)
  • 2020-12-01 22:53

    You cannot tell XSL 1.0 to fish a string out of a CDATA and parse it as XML.

    0 讨论(0)
提交回复
热议问题