XSLT将特定节点下的xml块转换为该节点的xml转义内容 [英] XSLT convert xml block under a specific node to xml-escaped content of that node
问题描述
INPUT:
< p为H.
< div>
原始< br />这是原始的< b>酸/羟基单羧酸< span class =hl1>酸< / span> ;.
< / div>
< / p>
期望的输出:
< p为H.
< div>
原始& lt / br /& gt;这是原始& lt; b& gt;酸& lt; / b& gt;羟基单羧酸& span class = HL1&安培; QUOT;&安培;氨基甲酸&安培; LT /跨度&安培; GT ;.
< / div>
< / p>
尝试1:
`< xsl:stylesheet version =2.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output omit-xml-declaration =yesindent =noencoding =UTF-8/>
<! - 身份模板 - >
< xsl:template match =node()| @ *>
< xsl:copy>
< xsl:apply-templates select =node()| @ */>
< / xsl:copy>
< / xsl:template>
< xsl:template match =div>
< xsl:copy>
< xsl:value-of select =/disable-output-escaping =no/>
< / xsl:copy>
< / xsl:template>
`
Attempt2:
作为替代,我想到将子元素的内容放入CDATA包装器中。
< xsl:stylesheet version = 2.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output omit-xml-declaration =yesindent =noencoding =UTF-8/>
<! - 身份模板 - >
< xsl:template match =node()| @ *>
< xsl:copy>
< xsl:apply-templates select =node()| @ */>
< / xsl:copy>
< / xsl:template>
< xsl:template match =div>
< xsl:copy>
< xsl:text disable-output-escaping =yes>& lt; lt;![CDATA [< / xsl:text>
< xsl:value-of select =//>
< xsl:text disable-output-escaping =yes>]]& gt< / xsl:text>
< / xsl:copy>
< / xsl:template>
但是没有给我我想要的是。
任何人有更好的主意?我使用XSLT 2.0
这是一个使用XSLT 3.0 serialize()
PE和EE:
< xsl:stylesheet version =3.0xmlns:xsl =http://www.w3 .org / 1999 / XSL / Transform
xmlns:xs =http://www.w3.org/2001/XMLSchema
exclude-result-prefixes =xs>
< xsl:template match =@ * | node()>
< xsl:copy>
< xsl:apply-templates select =@ * | node()/>
< / xsl:copy>
< / xsl:template>
< xsl:template match =div>
< xsl:copy>
< xsl:apply-templates mode =serialize/>
< / xsl:copy>
< / xsl:template>
< xsl:template match =node()mode =serialize>
< xsl:variable name =ser-params>
< output:serialization-parameters xmlns:output =http://www.w3.org/2010/xslt-xquery-serialization>
< output:omit-xml-declaration value =yes/>
< / output:serialization-parameters>
< / xsl:variable>
< xsl:value-of select =serialize(。,$ ser-params / *)/>
< / xsl:template>
< / xsl:stylesheet>
对于旧版本的Saxon 9,您可以使用扩展函数serialize,如 http://xsltransform.net/pPqsHTx :
<?xml version =1.0encoding =UTF-8?>
< xsl:stylesheet version =2.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform
xmlns:xs =http:// www。 w3.org/2001/XMLSchema
exclude-result-prefixes =xs>
< xsl:output name =inlineomit-xml-declaration =yes/>
< xsl:template match =@ * | node()>
< xsl:copy>
< xsl:apply-templates select =@ * | node()/>
< / xsl:copy>
< / xsl:template>
< xsl:template match =div>
< xsl:copy>
< xsl:apply-templates mode =serialize/>
< / xsl:copy>
< / xsl:template>
< xsl:template match =node()mode =serialize>
< xsl:value-of xmlns:saxon =http://saxon.sf.net/select =saxon:serialize(。,'inline')/>
< / xsl:template>
< / xsl:stylesheet>
Any ideas of how the following problem can be solved would be highly appreciated.
INPUT:
<p>
<div>
Original<br/>This is the original <b>acid</b>, a hydroxy monocarboxylic <span class="hl1">acid</span>.
</div>
</p>
Desired Output:
<p>
<div>
Original<br/>This is the original <b>acid</b>, a hydroxy monocarboxylic <span class="hl1">acid</span>.
</div>
</p>
Attempt 1:
`<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" indent="no" encoding="UTF-8"/>
<!--The identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:copy>
<xsl:value-of select="/" disable-output-escaping="no"/>
</xsl:copy>
</xsl:template>
`
Attempt2: as an alternative, I thought of placing the child elements' content into a CDATA wrapper.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" indent="no" encoding="UTF-8"/>
<!--The identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:copy>
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:value-of select="/" />
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:copy>
</xsl:template>
But that does not give me what I want. Anyone with a better idea? I'm using XSLT 2.0
Here is a suggestion using XSLT 3.0 serialize()
as supported by Saxon 9.6 HE, PE and EE:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:copy>
<xsl:apply-templates mode="serialize"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()" mode="serialize">
<xsl:variable name="ser-params">
<output:serialization-parameters xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization">
<output:omit-xml-declaration value="yes"/>
</output:serialization-parameters>
</xsl:variable>
<xsl:value-of select="serialize(., $ser-params/*)"/>
</xsl:template>
</xsl:stylesheet>
With older version of Saxon 9 you could use the extension function serialize, as shown in http://xsltransform.net/pPqsHTx:
<?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"
exclude-result-prefixes="xs">
<xsl:output name="inline" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:copy>
<xsl:apply-templates mode="serialize"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()" mode="serialize">
<xsl:value-of xmlns:saxon="http://saxon.sf.net/" select="saxon:serialize(., 'inline')"/>
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT将特定节点下的xml块转换为该节点的xml转义内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!