开发者

XSLT inject HTML

开发者 https://www.devze.com 2023-04-03 22:13 出处:网络
I have the following XML: <person> <name>John</name> <htmlDescription>A <strong>very</strong> <b><i>nice</i></b> person </htmlDescriptio

I have the following XML:

<person>
      <name>John</name>
      <htmlDescription>A <strong>very</strong> <b><i>nice</i></b> person </htmlDescription>
</person>

I would somehow like to use this XML in XSLT transform to produce HTML such as

A very nice person

is开发者_JS百科 this possible using XSLT?


If you transform it to HTML:

<xsl:stylesheet version='1.0'
                xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='html'/>

<xsl:template match='person'>
    <td>
        <xsl:copy-of select='htmlDescription/node()'/>
    </td>
</xsl:template>

</xsl:stylesheet>

If you transform it to XHTML:

<xsl:stylesheet version='1.0'
                xmlns='http://www.w3.org/1999/xhtml'
                xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='xml'/>

<xsl:template match='htmlDescription//text()'>
    <xsl:value-of select='.'/>
</xsl:template>

<xsl:template match='htmlDescription//*'>
    <xsl:element name='{local-name()}'>
        <xsl:copy-of select='@*'/>
        <xsl:apply-templates select='node()'/>
    </xsl:element>
</xsl:template>

<xsl:template match='person'>
    <td>
        <xsl:apply-templates select='htmlDescription/node()'/>
    </td>
</xsl:template>

</xsl:stylesheet>


Yes, it is possible. This example works for me:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="person" >
    <xsl:element name="td">
        <xsl:copy-of select="htmlDescription/*"  />
    </xsl:element>
</xsl:template>

</xsl:stylesheet>


<xsl:template match="person">
    <xsl:element name="td">
        <xsl:value-of select="htmlDescription"/>
    </xsl:element>   
</xsl:template>


<xsl:template match="person">
    <tr>
        <td>
            <xsl:value-of select="name"/>
        </td>
         <td>
            <xsl:value-of select="htmlDescription"/>
        </td> 
    </tr>  
</xsl:template>
0

精彩评论

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

关注公众号