XSLT从XML输出html标记

时间:2020-12-28 09:48:49

I have an XML document similar to:

我有一个类似于的XML文档:

<tag>
   <content>adsfasdf<b>asdf</b></content>
</tag>

I would like for the XSLT to select the content element and show all of the content:

我想让XSLT选择内容元素并显示所有内容:

<xsl:value-of select="/tag/content"/> 

The XSLT is configured to render as HTML. Is there a way that I can get the value-of/copy-of to display the exact content without having to render it?

XSLT配置为呈现为HTML。有没有办法让我可以获得值/副本来显示确切的内容而无需渲染它?

What I'm looking for is

我正在寻找的是

asdfasdf<b>asdf</b> 

And not:

并不是:

asdfasdf asdf

asdfasdf asdf

3 个解决方案

#1


10  

You need to escape the tag names within the content, I'd recommend something like:

您需要转义内容中的标记名称,我建议如下:

<xsl:template match="content//*">
    <xsl:value-of select="concat('&lt;',name(),'&gt;')"/>
    <xsl:apply-templates/>
    <xsl:value-of select="concat('&lt;/',name(),'&gt;')"/>
</xsl:template>

which you then can call with:

然后你可以打电话给:

<xsl:apply-templates select="/tag/content"/>

#2


1  

Quick and dirty way:

快速而肮脏的方式:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="content">
        <xsl:copy-of select="text() | *"/>
    </xsl:template>
</xsl:stylesheet>

Result against your sample will be:

针对您的样本的结果将是:

adsfasdf<b>asdf</b>

Another approach:

另一种方法:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="b">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

#3


1  

I would like for the XSLT to select the content element and show all of the content:

我想让XSLT选择内容元素并显示所有内容:

<xsl:value-of select="/tag/content"/> 

The XSLT is configured to render as HTML. Is there a way that I can get the value-of/copy-of to display the exact content without having to render it?

XSLT配置为呈现为HTML。有没有办法让我可以获得值/副本来显示确切的内容而无需渲染它?

What I'm looking for is

我正在寻找的是

asdfasdf<b>asdf</b>

And not:

并不是:

asdfasdf asdf

asdfasdf asdf

The answer of @Nick-Jones comes closest to what you want.

@ Nick-Jones的答案最接近你想要的。

Do have a look at the XSLT stylesheet that is part of the XPath Visualizer for an extensive example how an IE-style collapsible display of any XML document can be produced.

请查看作为XPath Visualizer一部分的XSLT样式表,以获取可以生成任何XML文档的IE样式可折叠显示的广泛示例。

#1


10  

You need to escape the tag names within the content, I'd recommend something like:

您需要转义内容中的标记名称,我建议如下:

<xsl:template match="content//*">
    <xsl:value-of select="concat('&lt;',name(),'&gt;')"/>
    <xsl:apply-templates/>
    <xsl:value-of select="concat('&lt;/',name(),'&gt;')"/>
</xsl:template>

which you then can call with:

然后你可以打电话给:

<xsl:apply-templates select="/tag/content"/>

#2


1  

Quick and dirty way:

快速而肮脏的方式:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="content">
        <xsl:copy-of select="text() | *"/>
    </xsl:template>
</xsl:stylesheet>

Result against your sample will be:

针对您的样本的结果将是:

adsfasdf<b>asdf</b>

Another approach:

另一种方法:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="b">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

#3


1  

I would like for the XSLT to select the content element and show all of the content:

我想让XSLT选择内容元素并显示所有内容:

<xsl:value-of select="/tag/content"/> 

The XSLT is configured to render as HTML. Is there a way that I can get the value-of/copy-of to display the exact content without having to render it?

XSLT配置为呈现为HTML。有没有办法让我可以获得值/副本来显示确切的内容而无需渲染它?

What I'm looking for is

我正在寻找的是

asdfasdf<b>asdf</b>

And not:

并不是:

asdfasdf asdf

asdfasdf asdf

The answer of @Nick-Jones comes closest to what you want.

@ Nick-Jones的答案最接近你想要的。

Do have a look at the XSLT stylesheet that is part of the XPath Visualizer for an extensive example how an IE-style collapsible display of any XML document can be produced.

请查看作为XPath Visualizer一部分的XSLT样式表,以获取可以生成任何XML文档的IE样式可折叠显示的广泛示例。