如何在xslt映射中将子记录数据从源复制到目标

时间:2014-08-14 13:34:18

标签: xml xslt xslt-1.0 biztalk

我有一个源xml,如下所示

<customerSource>
  <Name>abc</Name>
  <Account>123</Account>
  <AccountInfoSource>
    <AccountLocaiton>Florida-MI</AccountLocaiton>
    <AccountZip>12341</AccountZip>
    <AccountBank>BOA</AccountBank>
    <AccountBalance>0.0001</AccountBalance>
    <AccountStatus>Active</AccountStatus>
    .
    .
    .
    .
    .
  </AccountInfoSource>
  <Employed>Yes</Employed>
</customerSource>

我可以在AccountInfoSource中拥有任何元素。所以,我必须将AccountInfoSource记录数据复制到AccountInfoDestination,你能帮忙吗?

我尝试如下

<ns0:customerDestination>
  <ns0:Account>
    <xsl:value-of select="ns0:customerDestination/ns0:Account" />
  </ns0:Account>
  <ns0:AccountInfoDestination>
    <xsl:value-of select="ns0:customerDestination/ns0:AccountInfoSource/text()" />
  </ns0:AccountInfoDestination>
  <ns0:Employed>
    <xsl:value-of select="ns0:customerDestination/ns0:Employed" />
  </ns0:Employed>
</ns0:customerDestination>

输出应如下所示。

<ns0:customerDestination>
  <ns0:Account>123</ns0:Account>
  <ns0:AccountInfoDestination>
    <ns0:AccountLocaiton>Florida-MI</ns0:AccountLocaiton>
    <ns0:AccountZip>12341</ns0:AccountZip>
    <ns0:AccountBank>BOA</ns0:AccountBank>
    <ns0:AccountBalance>0.0001</ns0:AccountBalance>
    <ns0:AccountStatus>Active</ns0:AccountStatus>
    .
    .
    .
    .
    .
  </ns0:AccountInfoDestination>
  <ns0:Employed>Yes</ns0:Employed>
</ns0:customerDestination>

1 个答案:

答案 0 :(得分:1)

您使用xslt-1.0xslt-2.0标记了您的问题,这没有任何意义,因为这些标记是互斥的。下面的解决方案适用于两个版本的XSLT,因为XSLT 2.0处理器也可以处理XSLT 1.0文档。

最重要的模板:

<xsl:template match="AccountInfoSource/* | Account | Employed">

匹配AccountInfoSourceAccountEmployed元素的所有子元素节点。然后,它构造了以ns0:为前缀的新元素:

<xsl:element name="{concat('ns0:',local-name())}">

然后将这些元素的文本内容不加改变地复制到输出树。

<强>样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns0="http://www.namespace.com">

    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="UTF-8"/>

    <xsl:template match="/customerSource">
        <ns0:customerDestination>
            <xsl:apply-templates/>
        </ns0:customerDestination>
    </xsl:template>

    <xsl:template match="Name"/>

    <xsl:template match="AccountInfoSource">
        <ns0:AccountInfoDestination>
            <xsl:apply-templates/>
        </ns0:AccountInfoDestination>
    </xsl:template>

    <xsl:template match="AccountInfoSource/* | Account | Employed">
        <xsl:element name="{concat('ns0:',local-name())}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:transform>

XML输出

注意:您显示的预期输出格式不正确,因为前缀为ns0:,但未定义命名空间。

<ns0:customerDestination xmlns:ns0="http://www.namespace.com">
   <ns0:Account>123</ns0:Account>
   <ns0:AccountInfoDestination>
      <ns0:AccountLocaiton>Florida-MI</ns0:AccountLocaiton>
      <ns0:AccountZip>12341</ns0:AccountZip>
      <ns0:AccountBank>BOA</ns0:AccountBank>
      <ns0:AccountBalance>0.0001</ns0:AccountBalance>
      <ns0:AccountStatus>Active</ns0:AccountStatus>
   </ns0:AccountInfoDestination>
   <ns0:Employed>Yes</ns0:Employed>
</ns0:customerDestination>