嵌套条件循环(一对多)

时间:2019-05-10 14:28:30

标签: xslt biztalk

Input中有两个xml,InputMessagePart_0具有多个位置ID,InputMessagePart_1具有多个ItemMaster,那么我需要创建一个Output,其中每个位置ID我都需要拥有Item Master。

我已经编写了Xslt,它不在InputMessagePart_0(Record)级别上循环,并且仅采用第一个Location ID

XSlT1.0:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 s2 s1" version="1.0" xmlns:s0="http://Test.ItemMaster" xmlns:s2="http://schemas.microsoft.com/BizTalk/2003/aggschema" xmlns:s1="http://Test.Lookup" xmlns:ns0="http://Test.Out">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/s2:Root" />
  </xsl:template>
  <xsl:template match="/s2:Root">
    <ns0:Root>
      <xsl:for-each select="InputMessagePart_1/s0:Root/ItemMaster">

        <xsl:variable name="var:v1" select="../../../InputMessagePart_0/s1:Root/Record/LocationId" />
        <xsl:variable name="var:v2" select="ItemId" />
        <xsl:variable name="var:v3" select="ItemName" />
        <xsl:variable name="var:v4" select="Quantity" />
        <Detail>
          <LocationId>
            <xsl:value-of select="$var:v1" />
          </LocationId>
          <ItemId>
            <xsl:value-of select="$var:v2" />
          </ItemId>
          <ItemName>
            <xsl:value-of select="$var:v3" />
          </ItemName>
          <Qty>
            <xsl:value-of select="$var:v4" />
          </Qty>
        </Detail>
      </xsl:for-each>
    </ns0:Root>
  </xsl:template>
</xsl:stylesheet>

输入XML:

<ns0:Root xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003/aggschema">
    <InputMessagePart_0>
        <ns0:Root xmlns:ns0="http://Test.Lookup">
  <Record>

    <LocationId>12</LocationId>
  </Record>
  <Record>

    <LocationId>13</LocationId>
  </Record>
  <Record>

    <LocationId>14</LocationId>
  </Record>
</ns0:Root>
    </InputMessagePart_0>
    <InputMessagePart_1>
        <ns0:Root xmlns:ns0="http://Test.ItemMaster">
  <ItemMaster>
    <ItemId>123</ItemId>
    <ItemName>Knife</ItemName>
    <Quantity>1</Quantity>
  </ItemMaster>
  <ItemMaster>
    <ItemId>1234</ItemId>
    <ItemName>Knife1</ItemName>
    <Quantity>1</Quantity>
  </ItemMaster>
  <ItemMaster>
    <ItemId>1235</ItemId>
    <ItemName>Knife3</ItemName>
    <Quantity>1</Quantity>
  </ItemMaster>
</ns0:Root>
    </InputMessagePart_1>
</ns0:Root>

当前输出:

<ns0:Root xmlns:ns0="http://Test.Out">
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>123</ItemId>
        <ItemName>Knife</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>1234</ItemId>
        <ItemName>Knife1</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>1235</ItemId>
        <ItemName>Knife3</ItemName>
        <Qty>1</Qty>
    </Detail>
</ns0:Root>

所需的输出:

<ns0:Root xmlns:ns0="http://Test.Out">
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>123</ItemId>
        <ItemName>Knife</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>1234</ItemId>
        <ItemName>Knife1</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>1235</ItemId>
        <ItemName>Knife3</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>13</LocationId>
        <ItemId>123</ItemId>
        <ItemName>Knife</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>13</LocationId>
        <ItemId>1234</ItemId>
        <ItemName>Knife1</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>13</LocationId>
        <ItemId>1235</ItemId>
        <ItemName>Knife3</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>14</LocationId>
        <ItemId>123</ItemId>
        <ItemName>Knife</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>14</LocationId>
        <ItemId>1234</ItemId>
        <ItemName>Knife1</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>14</LocationId>
        <ItemId>1235</ItemId>
        <ItemName>Knife3</ItemName>
        <Qty>1</Qty>
    </Detail>
</ns0:Root>



Desired OutPut : 

<ns0:Root xmlns:ns0="http://Test.Out">
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>123</ItemId>
        <ItemName>Knife</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>1234</ItemId>
        <ItemName>Knife1</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>12</LocationId>
        <ItemId>1235</ItemId>
        <ItemName>Knife3</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>13</LocationId>
        <ItemId>123</ItemId>
        <ItemName>Knife</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>13</LocationId>
        <ItemId>1234</ItemId>
        <ItemName>Knife1</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>13</LocationId>
        <ItemId>1235</ItemId>
        <ItemName>Knife3</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>14</LocationId>
        <ItemId>123</ItemId>
        <ItemName>Knife</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>14</LocationId>
        <ItemId>1234</ItemId>
        <ItemName>Knife1</ItemName>
        <Qty>1</Qty>
    </Detail>
    <Detail>
        <LocationId>14</LocationId>
        <ItemId>1235</ItemId>
        <ItemName>Knife3</ItemName>
        <Qty>1</Qty>
    </Detail>
</ns0:Root>

1 个答案:

答案 0 :(得分:1)

您需要拥有xsl:for-each(或xsl:apply-templates)才能获得具有位置的Record元素。 (并且此xsl:for-each将包含xsl:for-each上的当前ItemMaster

尝试使用此XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 s2 s1" version="1.0" xmlns:s0="http://Test.ItemMaster" xmlns:s2="http://schemas.microsoft.com/BizTalk/2003/aggschema" xmlns:s1="http://Test.Lookup" xmlns:ns0="http://Test.Out">
  <xsl:output omit-xml-declaration="yes" method="xml" indent="yes" version="1.0" />

  <xsl:template match="/s2:Root">
    <ns0:Root>
      <xsl:for-each select="InputMessagePart_0/s1:Root/Record">
        <xsl:variable name="var:v1" select="LocationId" /> 
        <xsl:for-each select="../../../InputMessagePart_1/s0:Root/ItemMaster">
          <Detail>
            <LocationId>
              <xsl:value-of select="$var:v1" />
            </LocationId>
            <ItemId>
              <xsl:value-of select="ItemId" />
            </ItemId>
            <ItemName>
              <xsl:value-of select="ItemName" />
            </ItemName>
            <Qty>
              <xsl:value-of select="Quantity" />
            </Qty>
          </Detail>
        </xsl:for-each>
      </xsl:for-each>
    </ns0:Root>
  </xsl:template>
</xsl:stylesheet>

请注意,这里实际上并不需要匹配/的模板,因为XSLT的内置模板可以完成相同的工作。