Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT将多个源节点映射到同一目标节点_Xslt - Fatal编程技术网

XSLT将多个源节点映射到同一目标节点

XSLT将多个源节点映射到同一目标节点,xslt,Xslt,我们有一组订单,它们以单个XML的形式出现。其中一行是必需的,称为主线,每个订单只有一行,并且处于标题级别,没有任何重复。还有两种其他行类型不是强制性的,但可以对每个订单重复。下面是输入XML示例,输出只是行的集合 <xsl:template match="/"> <PoHeadersCollection> <xsl:apply-templates select="orderDetails/data/order" /> </PoHeade

我们有一组订单,它们以单个XML的形式出现。其中一行是必需的,称为主线,每个订单只有一行,并且处于标题级别,没有任何重复。还有两种其他行类型不是强制性的,但可以对每个订单重复。下面是输入XML示例,输出只是行的集合

<xsl:template match="/">
  <PoHeadersCollection>
    <xsl:apply-templates select="orderDetails/data/order" />
  </PoHeadersCollection>
</xsl:template>

<xsl:template match="order">
  <PoHeaders>
    <xsl:copy-of select="poNumber | storeNumber | itemNumber" />
    <PoLinesCollection>
      <!-- process all the different types of line in one go -->
      <xsl:apply-templates select="mainLine
                    | optionalLines/optionalLine
                    | customLines/customLine" />
    </PoLinesCollection>
  </PoHeaders>
</xsl:template>

<xsl:template match="mainLine | optionalLine | customLine">
  <PoLines>
    <xsl:apply-templates select="referenceNumber"/>
    <xsl:apply-templates select="duoiCode"/><!-- won't exist for main line -->
    <xsl:apply-templates select="basicInstallQuantity | orderQuantity"/>
    <xsl:apply-templates select="." mode="lineType" />
    <xsl:apply-templates select="basicInstallCostAmount | orderRetailAmount"/>
  </PoLines>
</xsl:template>

<xsl:template match="referenceNumber">
  <partNumber><xsl:apply-templates/></partNumber>
</xsl:template>

<xsl:template match="duoiCode">
  <Uom><xsl:apply-templates/></Uom>
</xsl:template>

<!-- etc. for other elements -->
所有行,包括主线、所有可选行和所有自定义行,都需要映射到每个订单目标上的同一元素

    <orderDetails>
   <data>
      <order>
         <poNumber>1234</poNumber>
         <storeNumber>35</storeNumber>
         <itemNumber>1895550045</itemNumber>
         <mainLine>
            <referenceNumber>I1</referenceNumber>
            <basicInstallCostAmount>100</basicInstallCostAmount>
            <basicInstallQuantity>12</basicInstallQuantity>
         </mainLine>
         <optionalLines>
            <optionalLine>
               <referenceNumber>OP10</referenceNumber>
               <duoiCode>EA</duoiCode>
               <orderQuantity>15</orderQuantity>
               <orderRetailAmount>200</orderRetailAmount>
            </optionalLine>
            <optionalLine>
               <referenceNumber>OP105</referenceNumber>
               <duoiCode>EA</duoiCode>
               <orderQuantity>23</orderQuantity>
               <orderRetailAmount>655</orderRetailAmount>
            </optionalLine>
         </optionalLines>
         <customLines>
            <customLine>
               <referenceNumber>C100</referenceNumber>
               <duoiCode>EA</duoiCode>
               <orderQuantity>123</orderQuantity>
               <orderRetailAmount>12300</orderRetailAmount>
            </customLine>
            <customLine>
               <referenceNumber>C1337</referenceNumber>
               <duoiCode>EA</duoiCode>
               <orderQuantity>357</orderQuantity>
               <orderRetailAmount>143</orderRetailAmount>
            </customLine>
         </customLines>
      </order>
      <order>
         <poNumber>5678</poNumber>
         <storeNumber>52</storeNumber>
         <itemNumber>0005554433</itemNumber>
         <mainLine>
            <referenceNumber>I21</referenceNumber>
            <basicInstallCostAmount>3000</basicInstallCostAmount>
            <basicInstallQuantity>35</basicInstallQuantity>
         </mainLine>
         <optionalLines>
            <optionalLine>
               <referenceNumber>OP134</referenceNumber>
               <duoiCode>EA</duoiCode>
               <orderQuantity>1500</orderQuantity>
               <orderRetailAmount>350000</orderRetailAmount>
            </optionalLine>
         </optionalLines>
         <customLines>
            <customLine>
               <referenceNumber>C140</referenceNumber>
               <duoiCode>EA</duoiCode>
               <orderQuantity>13</orderQuantity>
               <orderRetailAmount>100</orderRetailAmount>
            </customLine>
         </customLines>
      </order>
   </data>
</orderDetails>

1234
35
1895550045
I1
100
12
凤凰社第10章
每个
15
200
凤凰社105
每个
23
655
C100
每个
123
12300
C1337
每个
357
143
5678
52
0005554433
I21
3000
35
OP134
每个
1500
350000
C140
每个
13
100
预期产量-

<PoHeadersCollection>
   <PoHeaders>
      <poNumber>1234</poNumber>
      <storeNumber>35</storeNumber>
      <itemNumber>1895550045</itemNumber>
      <PoLinesCollection>
         <PoLines>
            <partNumber>I1</partNumber>
            <Quantity>12</Quantity>
            <installType>Basic</installType>
            <lineAmount>100</lineAmount>
         </PoLines>
         <PoLines>
            <partNumber>OP10</partNumber>
            <Uom>EA</Uom>
            <Quantity>15</Quantity>
            <installType>Optional</installType>
            <lineAmount>200</lineAmount>
         </PoLines>
         <PoLines>
            <partNumber>OP105</partNumber>
            <Uom>EA</Uom>
            <Quantity>23</Quantity>
            <installType>Optional</installType>
            <lineAmount>655</lineAmount>
         </PoLines>
         <PoLines>
            <partNumber>C100</partNumber>
            <Uom>EA</Uom>
            <Quantity>123</Quantity>
            <installType>Custom</installType>
            <lineAmount>12300</lineAmount>
         </PoLines>
         <PoLines>
            <partNumber>C1337</partNumber>
            <Uom>EA</Uom>
            <Quantity>357</Quantity>
            <installType>Custom</installType>
            <lineAmount>143</lineAmount>
         </PoLines>
      </PoLinesCollection>
   </PoHeaders>
   <PoHeaders>
      <poNumber>5678</poNumber>
      <storeNumber>52</storeNumber>
      <itemNumber>0005554433</itemNumber>
      <PoLinesCollection>
         <PoLines>
            <partNumber>I21</partNumber>
            <Quantity>35</Quantity>
            <installType>Basic</installType>
            <lineAmount>3000</lineAmount>
         </PoLines>
         <PoLines>
            <partNumber>OP134</partNumber>
            <Uom>EA</Uom>
            <Quantity>1500</Quantity>
            <installType>Optional</installType>
            <lineAmount>350000</lineAmount>
         </PoLines>
         <PoLines>
            <partNumber>C140</partNumber>
            <Uom>EA</Uom>
            <Quantity>13</Quantity>
            <installType>Custom</installType>
            <lineAmount>100</lineAmount>
         </PoLines>
      </PoLinesCollection>
   </PoHeaders>
</PoHeadersCollection>

1234
35
1895550045
I1
12
基本的
100
凤凰社第10章
每个
15
可选择的
200
凤凰社105
每个
23
可选择的
655
C100
每个
123
风俗
12300
C1337
每个
357
风俗
143
5678
52
0005554433
I21
35
基本的
3000
OP134
每个
1500
可选择的
350000
C140
每个
13
风俗
100
我尝试使用for each,并在循环中尝试将模板应用于其余线型,但它正在重复所有节点

下面是我试图使用的XSLT,但在如何在for循环中单独为当前节点应用模板方面却没有-

<xsl:stylesheet version="1.0">
  <xsl:template match="/">
    <PoHeadersCollection>
      <xsl:for-each select="/orderDetails/data/order">
        <ThdIconxPoHeaders>
          <poNumber>
            <xsl:value-of select="poNumber"/>
          </poNumber>
          <storeNumber>
            <xsl:value-of select="storeNumber"/>
          </storeNumber>
          <itemNumber>
            <xsl:value-of select="itemNumber"/>
          </itemNumber>
          <PoLinesCollection>
            <PoLines>
              <partNumber>
                <xsl:value-of select="mainLine/referenceNumber"/>
              </partNumber>
              <Uom>
                <xsl:value-of select="mainLine/duoiCode"/>
              </Uom>
              <Quantity>
                <xsl:value-of select="mainLine/basicInstallQuantity"/>
              </Quantity>
              <installType>
                <xsl:text disable-output-escaping="no">Basic</xsl:text>
              </installType>
              <lineAmount>
                <xsl:value-of select="mainLine/basicInstallCostAmount"/>
              </lineAmount>
            </PoLines>          
            <xsl:apply-templates select="//*[local-name()='optionalLine']"/>
            <xsl:apply-templates select="//*[local-name()='customLine']"/>
          </PoLinesCollection>
        </PoHeaders>
      </xsl:for-each>
    </PoHeadersCollection>
  </xsl:template>
  <xsl:template match="*[local-name()='optionalLine']">
    <PoLines>
      <partNumber>
        <xsl:value-of select="referenceNumber"/>
      </partNumber>
      <Uom>
        <xsl:value-of select="duoiCode"/>
      </Uom>
      <Quantity>
        <xsl:value-of select="orderQuantity"/>
      </Quantity>
      <installType>
        <xsl:text disable-output-escaping="no">Optional</xsl:text>
      </installType>
    </PoLines>
  </xsl:template>
  <xsl:template match="*[local-name()='customLine']">
    <PoLines>
      <partNumber>
        <xsl:value-of select="referenceNumber"/>
      </partNumber>
      <Uom>
        <xsl:value-of select="duoiCode"/>
      </Uom>
      <Quantity>
        <xsl:value-of select="orderQuantity"/>
      </Quantity>
      <installType>
        <xsl:text disable-output-escaping="no">Custom</xsl:text>
      </installType>
    </PoLines>
  </xsl:template>
</xsl:stylesheet>

基本的
可选择的
风俗
请建议

提前感谢,,
Geeta

您的问题是绝对路径,例如

<xsl:apply-templates select="//*[local-name()='optionalLine']"/>

请显示您的xslt Hello Jim,我添加了我一直使用的xslt。谢谢。谢谢你,伊恩,我确实编辑了这个问题,加入了我一直在使用的XSLT。实际的XML几乎有10个这样的重复节点,每个节点大约有50个元素。同样,这些元素中有一半在重复节点之间并不常见。@user3352765我已经对其进行了编辑,以解释为什么当前样式表会出现重复,但我仍然建议将其分解为更小、更可重用的部分。如果您决定在输出中使用
quantity
而不是
quantity
,则只需修改一个模板,而不是3个。谢谢!相对XPATH确实解决了这个问题。我会按照你的建议把它分成可重复使用的部分。谢谢你的帮助!!!
<xsl:template match="/">
  <PoHeadersCollection>
    <xsl:apply-templates select="orderDetails/data/order" />
  </PoHeadersCollection>
</xsl:template>

<xsl:template match="order">
  <PoHeaders>
    <xsl:copy-of select="poNumber | storeNumber | itemNumber" />
    <PoLinesCollection>
      <!-- process all the different types of line in one go -->
      <xsl:apply-templates select="mainLine
                    | optionalLines/optionalLine
                    | customLines/customLine" />
    </PoLinesCollection>
  </PoHeaders>
</xsl:template>

<xsl:template match="mainLine | optionalLine | customLine">
  <PoLines>
    <xsl:apply-templates select="referenceNumber"/>
    <xsl:apply-templates select="duoiCode"/><!-- won't exist for main line -->
    <xsl:apply-templates select="basicInstallQuantity | orderQuantity"/>
    <xsl:apply-templates select="." mode="lineType" />
    <xsl:apply-templates select="basicInstallCostAmount | orderRetailAmount"/>
  </PoLines>
</xsl:template>

<xsl:template match="referenceNumber">
  <partNumber><xsl:apply-templates/></partNumber>
</xsl:template>

<xsl:template match="duoiCode">
  <Uom><xsl:apply-templates/></Uom>
</xsl:template>

<!-- etc. for other elements -->
<xsl:template match="mainLine" mode="lineType">
  <installType>Basic</installType>
</xsl:template>

<xsl:template match="optionalLine" mode="lineType">
  <installType>Optional</installType>
</xsl:template>

<xsl:template match="customLine" mode="lineType">
  <installType>Custom</installType>
</xsl:template>