【问题标题】:How to convert xml to string using xslt如何使用 xslt 将 xml 转换为字符串
【发布时间】:2018-08-29 12:44:48
【问题描述】:

我想在XML下面转换

<Root>
  <instruction>
    <header>
      <type>A</type>
      <date>29-08-2018</date>
    </header>
    <message>
      <name>parth</name>
      <age>24</age>
    </message>
  </instruction>
</Root>

使用 XSLT 到 XML 下方

<Root>
  <request>
    <instruction>
      <header>
        <type>A</type>
        <date>29-08-2018</date>
      </header>
      <message>
        <name>parth</name>
        <age>24</age>
      </message>
    </instruction>
  </request>
</Root>

在上面的输出中,&lt;request&gt; 标签内的所有标签都是字符串形式,而不是 XML 元素。有什么建议吗?

【问题讨论】:

  • 原始文件中的字段较多,需要在输出映射中具有整个xml结构。请指教。
  • 您的输入 XML 格式不正确,因为它有一个开始 &lt;instruction&gt; 标记,但没有结束 &lt;/instruction&gt;。您能否更正问题中的 XML,否则将无法对其应用 XSLT。你也能说一下你想要实现的逻辑吗?您只是想添加request 节点吗?
  • Good Day Tim 我检查并更正了它,请立即查看
  • 我指的是输入 XML,它缺少结束标记。此外,您能否确认您正在尝试将request 标记添加到 XML 中?谢谢
  • 你也说“&lt;request&gt;标签内的标签是字符串形式的”。您的意思是在这种情况下要“转义”所有节点,还是将它们作为 CDATA 输出。如果是这种情况,您应该显示您想要的实际 XML。谢谢。

标签: xml xslt biztalk-2010 biztalk-mapper


【解决方案1】:

根据您的问题中显示的内容,我会这样编写 XSLT:

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

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

这将输出 XML,如您的问题所示。见http://xsltfiddle.liberty-development.net/pPqsHTL

注意在复制现有元素时使用标识模板。

但是,从您的 cmets 看来,您希望将元素转换为文本,这是通过“转义”它们来实现的。如果是这种情况,我会这样写 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" cdata-section-elements="request" />

  <xsl:template match="*">
    <xsl:value-of select="concat('&lt;', name())" />
    <xsl:for-each select="@*">
      <xsl:value-of select="concat(' ', name(), '=&quot;', ., '&quot;')" />
    </xsl:for-each>
    <xsl:text>&gt;</xsl:text>
    <xsl:apply-templates />
    <xsl:value-of select="concat('&lt;/', name(), '&gt;')" />
  </xsl:template>

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

这会输出以下内容

<Root>
   <request><![CDATA[
  <instruction a="1">
    <header>
      <type>A</type>
      <date>29-08-2018</date>
    </header>
    <message>
      <name>parth</name>
      <age>24</age>
    </message>
  </instruction>
]]></request>
</Root>

http://xsltfiddle.liberty-development.net/nc4NzQJ

【讨论】:

  • 感谢蒂姆的解决方案。当它尝试在 BizTalk 脚本函数中应用它时,我遇到了一些错误。请在下面找到请求 xml(source) ManInit">
    Age
    CRef Man>
  • 错误 3 XSL 转换错误:'xsl:template' 不能是 'xsl:template' 元素的子元素。
  • 恐怕我不懂BizTalk,所以不知道如何使用它来应用XSLT。
  • 您能否就错误提出建议。这可能会有所帮助
  • 我想我可能知道为什么它会给出错误如果源 xml 目标命名空间是“tempuri.com”并且目标 xml 命名空间是“http:gootemp.com”,请告诉我如何进行此转换"
猜你喜欢
  • 2011-10-05
  • 2018-03-09
  • 2014-01-03
  • 2011-02-13
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 2012-10-12
  • 2021-07-22
相关资源
最近更新 更多