0

Well, for example I have an 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>

and part of 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>

For every $WepNr value I want to copy whole <PurchaseOrderLine> and insert <LineNumber> with current WepNr value.

So, for example: if $WepNr for 1st PurchaseOrderLine returns: 16; 26 result will be:

<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>

Is it possible?

UPD: DBresponse XML part

<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>

It just means that WepNr could return multiple values: like "1 16" in this case

2
  • Please show us the input sample with elements you are trying to select in /*/DbResponse/ResultSet/Row[Cell[@name='ARTNR']=$ArtNr][Cell[@name='WEANR']=$WeaNr]/Cell[@name='WEPNR']. Oct 8, 2012 at 11:12
  • @MartinHonnen question was updated
    – VextoR
    Oct 8, 2012 at 11:18

1 Answer 1

1

I think instead of

 <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>

you rather want:

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

and then at the top level

 <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>

That assumes that $WearNr is a global param or variable, otherwise you would need to pass that value on e.g.

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

and

 <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>

All untested of course, you would better supply minimal but complete code sample allowing us to write testable code.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.