XSLT 将根节点放入目标架构的字符串元素中



我在尝试解决我面临的问题时遇到了很多困难。我们有一个源 XML 架构,我们正在使用 XSLT 将其转换为目标架构。但是,目标架构中的一个元素旨在保存源 XML(包括属性)中的原始 XML。我不希望使用 CDATA,因为这会在再次使用数据时导致问题。我在 BizTalk 2009 中运行此 XSLT,因此我只能使用 XSLT 1.0/XPATH 1.0。

哦,更复杂的是,源 XML 中的数据在某些元素中具有<和>。

来源示例:

<root>
<foo company="1">
    <bar id="125" title="foobar3">
    &gt; 15 years
</bar>
    <bar id="126" title="foobar4">
    &lt; 5 years
</bar>
</foo>
<foo company="2">
    <bar id="125" title="foobar3">
    &gt; 15 years
</bar>
    <bar id="126" title="foobar4">
    &lt; 5 years
</bar>
</foo>

示例目标

<newXML>
    <Company>1</Company>
    <SourceXML>
&lt;root&gt;
    &lt;foo company="1"&gt;
        &lt;bar id="125" title="foobar3"&gt;
        "&gt;" 15 years
    &lt;/bar&gt;
        &lt;bar id="126" title="foobar4"&gt;
        "&lt;" 5 years
    &lt;/bar&gt;
    &lt;/foo&gt;
    &lt;foo company="2"&gt;
        &lt;bar id="125" title="foobar3"&gt;
        "&gt;" 15 years
    &lt;/bar&gt;
        &lt;bar id="126" title="foobar4"&gt;
        "&lt;" 5 years
    &lt;/bar&gt;
    &lt;/foo&gt;
&lt;/root&gt;   
    </SourceXML>
</newXML>

没那么复杂。您只需为元素和属性节点编写两个模板,将所需的文本表示形式写入输出树。只有在文本节点中用引号包装<>才需要更多的键入。样式表可能如下所示:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <newXML>
         <Company>1</Company>
         <SourceXML>
            <xsl:apply-templates/>
         </SourceXML>
      </newXML>
   </xsl:template>
   <xsl:template match="*">
      <xsl:value-of select="concat('&lt;',name())"/>
      <xsl:apply-templates select="@*"/>
      <xsl:text>&gt;</xsl:text>
      <xsl:apply-templates select="node()"/>
      <xsl:value-of select="concat('&lt;/',name(), '>')"/>
   </xsl:template>
   <xsl:template match="@*">
      <xsl:value-of select="concat(' ', name(),'=','&quot;', ., '&quot;')"/>
   </xsl:template>
   <xsl:template match="text()">
      <xsl:call-template name="wrap">
         <xsl:with-param name="str">
            <xsl:call-template name="wrap">
               <xsl:with-param name="str" select="."/>
               <xsl:with-param name="find" select="'&lt;'"/>
            </xsl:call-template>
         </xsl:with-param>
         <xsl:with-param name="find" select="'&gt;'"/>
      </xsl:call-template>
   </xsl:template>
   <xsl:template name="wrap">
      <xsl:param name="str"/>
      <xsl:param name="find"/>
      <xsl:choose>
         <xsl:when test="contains($str, $find)">
            <xsl:variable name="before" select="substring-before($str, $find)"/>
            <xsl:variable name="after" select="substring-after($str, $find)"/>
            <xsl:value-of select="concat($before, '&quot;', $find, '&quot;')"/>
            <xsl:call-template name="wrap">
               <xsl:with-param name="str" select="$after"/>
               <xsl:with-param name="find" select="$find"/>
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$str"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

最新更新