XSLT convert xml block under a specific node to xm

2019-09-19 05:55发布

问题:

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&lt;br/&gt;This is the original &lt;b&gt;acid&lt;/b&gt;, a hydroxy monocarboxylic &lt;span class=&quot;hl1&quot;&gt;acid&lt;/span&gt;.
    </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">&lt;![CDATA[</xsl:text>
            <xsl:value-of select="/" />
            <xsl:text disable-output-escaping="yes">]]&gt;</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

回答1:

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>


回答2:

Your second attempt should work if you change your xsl:value-of to an xsl:copy-of and tweak the select:

<xsl:template match="div">
    <xsl:copy>
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:copy-of select="node()" />
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
    </xsl:copy>
</xsl:template>