0

好吧,例如我有一个 XML:

<ProcessPurchaseOrder >
    <PurchaseOrderLine>
        <DocumentReference type="sendersReference2">
            <DocumentID>
                <ID>100</ID>
            </DocumentID>
        </DocumentReference>
        <DocumentReference type="sendersReference3">
            <DocumentID>
                <ID>ru</ID>
            </DocumentID>
        </DocumentReference>
        <Item>
            <CustomerItemID>
                <ID>00126</ID>
            </CustomerItemID>
        </Item>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
        <DocumentReference type="sendersReference2">
            <DocumentID>
                <ID>200</ID>
            </DocumentID>
        </DocumentReference>
        <DocumentReference type="sendersReference3">
            <DocumentID>
                <ID>ru</ID>
            </DocumentID>
        </DocumentReference>
        <Item>
            <CustomerItemID>
                <ID>123122</ID>
            </CustomerItemID>
        </Item>
    </PurchaseOrderLine>
</ProcessPurchaseOrder>

和 XSLT 的一部分:

 <xsl:for-each select="*:PurchaseOrderLine">
    <xsl:variable name="ArtNr" select="*:Item/*:CustomerItemID/*:ID"/>
    <xsl:variable name="WepNr" select="/*/DbResponse/ResultSet/Row[Cell[@name='ARTNR']=$ArtNr][Cell[@name='WEANR']=$WeaNr]/Cell[@name='WEPNR']"/>
    <xsl:copy>
        <xsl:if test="$WepNr!=''">
            <xsl:for-each select="$WepNr">
                <LineNumber><xsl:value-of select="$WepNr/current()"/></LineNumber>
            </xsl:for-each>
        </xsl:if>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:for-each>

对于$WepNr我想复制整个值<PurchaseOrderLine>并插入<LineNumber>当前值的每个WepNr值。

因此,例如:如果$WepNr第一个 PurchaseOrderLine 返回:16;26 结果将是:

<ProcessPurchaseOrder >
    <PurchaseOrderLine>
    <!-- wepnr[1] = 16 -->
    <LineNumber>16</LineNumber>
        <DocumentReference type="sendersReference2">
            <DocumentID>
                <ID>100</ID>
            </DocumentID>
        </DocumentReference>
        <DocumentReference type="sendersReference3">
            <DocumentID>
                <ID>ru</ID>
            </DocumentID>
        </DocumentReference>
        <Item>
            <CustomerItemID>
                <ID>00126</ID>
            </CustomerItemID>
        </Item>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
    <!-- copied PurchaseOrderLine with wepnr[2]=26 -->
    <LineNumber>26</LineNumber>
        <DocumentReference type="sendersReference2">
            <DocumentID>
                <ID>100</ID>
            </DocumentID>
        </DocumentReference>
        <DocumentReference type="sendersReference3">
            <DocumentID>
                <ID>ru</ID>
            </DocumentID>
        </DocumentReference>
        <Item>
            <CustomerItemID>
                <ID>00126</ID>
            </CustomerItemID>
        </Item>
    </PurchaseOrderLine>

    <!-- here is 2nd PurchaseOrderLine>
    <!-- ... -->

</ProcessPurchaseOrder>

是否可以?

UPD:DBresponse XML 部分

<DbResponse>
   <ResultSet>
      <Row>
         <Cell name="WEANR" type="VARCHAR2">1909123</Cell>
         <Cell name="ARTNR" type="VARCHAR2">00126</Cell>
         <Cell name="WEPNR" type="VARCHAR2">1</Cell>
      </Row>
      <Row>
         <Cell name="WEANR" type="VARCHAR2">1909123</Cell>
         <Cell name="ARTNR" type="VARCHAR2">00126</Cell>
         <Cell name="WEPNR" type="VARCHAR2">16</Cell>
      </Row>
   </ResultSet>
</DbResponse>

这只是意味着WepNr可以返回多个值:1 16在这种情况下,例如“”

4

1 回答 1

1

我认为而不是

 <xsl:for-each select="*:PurchaseOrderLine">
    <xsl:variable name="ArtNr" select="*:Item/*:CustomerItemID/*:ID"/>
    <xsl:variable name="WepNr" select="/*/DbResponse/ResultSet/Row[Cell[@name='ARTNR']=$ArtNr][Cell[@name='WEANR']=$WeaNr]/Cell[@name='WEPNR']"/>
    <xsl:copy>
        <xsl:if test="$WepNr!=''">
            <xsl:for-each select="$WepNr">
                <LineNumber><xsl:value-of select="$WepNr/current()"/></LineNumber>
            </xsl:for-each>
        </xsl:if>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:for-each>

你宁愿:

 <xsl:apply-templates select="*:PurchaseOrderLine"/>

然后在顶层

 <xsl:key name="row-by-wepnr" 
   match="DbResponse/ResultSet/Row"
   use="Cell[@name='ARTNR']"/>


 <xsl:template match="*:PurchaseOrderLine">
   <xsl:variable name="this" select="."/>
   <xsl:variable name="wepNrs" select="key('row-by-wepnr', *:Item/*:CustomerItemID/*:ID)[Cell[@name='WEANR']=$WeaNr]/Cell[@name='WEPNR']"/>
   <xsl:for-each select="$wepNrs">
     <xsl:apply-templates select="$this" mode="add-wep">
       <xsl:with-param name="wep" select="current()"/>
     </xsl:apply-templates>
   </xsl:for-each>
 </xsl:template>

 <xsl:template match="*:PurchaseOrderLine" mode="add-wep">
    <xsl:param name="wep"/>
    <xsl:copy>
      <LineNumber><xsl:value-of select="$wep"/></LineNumber>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

假设这$WearNr是一个全局参数或变量,否则您需要将该值传递给例如

 <xsl:apply-templates select="*:PurchaseOrderLine">
   <xsl:with-param name"WearNr" select="$WearNr"/>
  </xsl:apply-templates>

 <xsl:template match="*:PurchaseOrderLine">
   <xsl:param name="$WearNr"/>
   <xsl:variable name="this" select="."/>
   <xsl:variable name="wepNrs" select="key('row-by-wepnr', *:Item/*:CustomerItemID/*:ID)[Cell[@name='WEANR']=$WeaNr]/Cell[@name='WEPNR']"/>
   <xsl:for-each select="$wepNrs">
     <xsl:apply-templates select="$this" mode="add-wep">
       <xsl:with-param name="wep" select="current()"/>
     </xsl:apply-templates>
   </xsl:for-each>
 </xsl:template>

 <xsl:template match="*:PurchaseOrderLine" mode="add-wep">
    <xsl:param name="wep"/>
    <xsl:copy>
      <LineNumber><xsl:value-of select="$wep"/></LineNumber>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

当然,所有未经测试的,您最好提供最少但完整的代码示例,以便我们编写可测试的代码。

于 2012-10-08T11:58:20.037 回答