当前位置: 首页 > 知识库问答 >
问题:

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

南门鸿振
2023-03-14

我很难解决我面临的一个问题。我们有一个源XML模式,我们正在使用XSLT将其转换为目标模式。然而,目标模式中的一个元素被设计为保存源XML的原始XML(包括atributes)。我不希望使用CDATA,因为当再次使用数据时,这将导致问题。我正在BizTalk2009中运行此XSLT,因此我将仅限于使用XSLT1.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>

共有1个答案

魏凯捷
2023-03-14

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

<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>
 类似资料:
  • 我需要在一个映射中插入一些数据,该映射需要一个字符串作为键,一个字符串列表作为值,但我不知道怎么做。 这是我试图做的。 首先,我创建了一个HashMap,然后创建了一个新对象,现在出现了问题。 我创建了一个新的字符串列表,给它起了一个名字,然后我认为我必须使用“put”方法,但这是错误的,因为我有一个错误,告诉你:“方法put(String,List)在类型HashMap中 为什么是布尔值?当我在

  • 我是xslt新手。 请参阅以下案例: 输入: 案例1: Xslt代码: 案例1的输出: 案例2: Xslt代码: 在情况2中,没有显示输出。我不明白为什么? 当它指向根节点Hello时。因此,在案例1中,当它执行时 这将打印整个xml。 案例2:执行时 它应该打印在下面。 有人能帮忙吗? 谢谢,Shashiraj NK

  • 当我使用将新节点插入到二叉树中时,它只在子节点的位置插入新节点,即使根节点已经有左、右子节点。它不是访问子节点来创建更深层次的二叉树。 抱歉英语不好。

  • 问题内容: 有没有一种pythonic方式将元素插入字符串中的每个第二个元素? 我有一个字符串:“ aabbccdd”,我希望最终结果是“ aa-bb-cc-dd”。 我不确定该怎么做。 问题答案: 假设字符串的长度始终是偶数, 该也可以消除 算法是将字符串分组为对,然后将其与字符连接在一起。 代码是这样写的。首先,它分为奇数位和偶数位。 然后使用该函数将它们组合成一个可迭代的元组。 但是元组不是

  • 问题内容: 我很难找到最简单的方法来针对给定的JSON模式字符串验证JSON字符串(作为参考,这是在Java中运行在Android应用程序中)。 理想情况下,我只想传入JSON字符串和JSON模式字符串,并且它返回关于是否通过验证的布尔值。通过搜索,我发现了以下两个有前途的库可以完成此任务: http://jsontools.berlios.de/ https://github.com/fge/j