cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

xslt code help needed

Former Member
0 Likes
572

Dear SAP experts,

Would you be able to help me in configuring the right xslt code to accomodate the looping logic?

Source Document:

School (occur only once)

- Name (can occur multiple times)

- Nickname (occur only once)

- Desired name (occur only once)

Target Document:

School

- Name

- Nickname (the value for this should be obtained from 'Desired name field')

- Desired name

I have this code, but seems not working in looping logic. (to accomodate the multiple Names):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" media-type="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="@*|node()">

<xsl:copy>

<xsl:apply-templates select="@*|node()"/>

</xsl:copy>

</xsl:template>

<xsl:template match="Order/OrderDetail/ListOfItemDetail/ItemDetail/BaseItemDetail/ItemIdentifiers/PartNumbers/BuyerPartNumber">

<BuyerPartNumber>

<PartNum>

<PartID>

<xsl:value-of select ="/Order/OrderDetail/ListOfItemDetail/ItemDetail/BaseItemDetail/ItemIdentifiers/PartNumbers/ManufacturerPartNumber/PartID"/>

</PartID>

</PartNum>

</BuyerPartNumber>

</xsl:template>

</xsl:stylesheet>

What happened is that the value was taken ONLY on the 1st line item. (1st Name occurence)

The succeeding Name field just copied the value of Nicknames from the 1st Name field.

Kindly advise how to handle the context (loop logic) in the xslt code.

Thanks!

View Entire Topic
Former Member
0 Likes

Apologies, here's the logic for the source document I've mentioned above.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" media-type="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="@*|node()">

<xsl:copy>

<xsl:apply-templates select="@*|node()"/>

</xsl:copy>

</xsl:template>

<xsl:template match="Name/Nickname">

<BuyerPartNumber>

<PartNum>

<PartID>

<xsl:value-of select ="Name/Desired name"/>

</PartID>

</PartNum>

</BuyerPartNumber>

</xsl:template>

</xsl:stylesheet>

I will consider your inputs and retest again. I will inform for any updates,

Thanks for your inputs.

Former Member
0 Likes

Hi Gerberto,

If your template matches Name/Nickname you cannot access Name/DesiredName the way you have shown. If you give the full path /School/Name/DesiredName you will only get the first occurrence. Try getting the value using a context aware xpath like in the example below. This will copy source to target as-is except for the Name/Nickname field. The Nickname value will be replaced with the value from DesiredName within the same "Name" context.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" media-type="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="Name/Nickname">
    <Nickname><xsl:value-of select="../DesiredName"/></Nickname>
  </xsl:template>
  
</xsl:stylesheet>

If you want to rebuild the entire "Name" node then set your template to match on Name and access fields within Name as follows:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" media-type="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Name">
    <Name>
      <DesiredName><xsl:value-of select="DesiredName"/></DesiredName>
      <Nickname><xsl:value-of select="DesiredName"/></Nickname>
    </Name>
  </xsl:template>

</xsl:stylesheet>

Thanks,

-Russ