0

i have an xml like this

<SyncItemMaster>
    <DataArea>
        <Sync>
            <TenantID>Infor</TenantID>
            <AccountingEntityID/>
            <LocationID/>
            <ActionCriteria>
                <ActionExpression actionCode="Replace"/>
            </ActionCriteria>
        </Sync>
        <ItemMaster>
            <ItemMasterHeader>
            <ItemID>
            <ID>frome</ID>
            </ItemID>
                <UserArea>
                    <Property>
                        <NameValue>extra field</NameValue>
                    </Property>
                </UserArea>
            </ItemMasterHeader>
        </ItemMaster>
    </DataArea>
</SyncItemMaster>

and my xslt is this

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">

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

<xsl:template match="ItemMasterHeader/UserArea/Property">
<xsl:variable name="item" select="//ID" />
    <Property>
<NameValue>/Item_Image[@Item_Name = "<xsl:value-of select="$item"/>"]</NameValue>
</Property> 
    <xsl:copy-of select="."/>
</xsl:template>


</xsl:stylesheet>

this code works fine if there is the UserArea tag and return this xml

<?xml version="1.0" encoding="UTF-8"?>
<SyncItemMaster>
   <DataArea>
      <Sync>
         <TenantID>Infor</TenantID>
         <AccountingEntityID/>
         <LocationID/>
         <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
         </ActionCriteria>
      </Sync>
      <ItemMaster>
         <ItemMasterHeader>
            <ItemID>
               <ID>frome</ID>
            </ItemID>
            <UserArea>
               <Property>
                  <NameValue>/Item_Image[@Item_Name = "frome"]</NameValue>
               </Property>
               <Property>
                  <NameValue>extra field</NameValue>
               </Property>
            </UserArea>
         </ItemMasterHeader>
      </ItemMaster>
   </DataArea>
</SyncItemMaster>

what I would like to have is the following XML if in the original there is not the UserArea tag so if my input is this

<SyncItemMaster>
    <DataArea>
        <Sync>
            <TenantID>Infor</TenantID>
            <AccountingEntityID/>
            <LocationID/>
            <ActionCriteria>
                <ActionExpression actionCode="Replace"/>
            </ActionCriteria>
        </Sync>
        <ItemMaster>
            <ItemMasterHeader>
            <ItemID>
               <ID>frome</ID>
            </ItemID>
            </ItemMasterHeader>
        </ItemMaster>
    </DataArea>
</SyncItemMaster>

i would like to get this

<SyncItemMaster>
   <DataArea>
      <Sync>
         <TenantID>Infor</TenantID>
         <AccountingEntityID/>
         <LocationID/>
         <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
         </ActionCriteria>
      </Sync>
      <ItemMaster>
         <ItemMasterHeader>
            <ItemID>
               <ID>frome</ID>
            </ItemID>
            <UserArea>
               <Property>
                  <NameValue>/Item_Image[@Item_Name = "frome"]</NameValue>
               </Property>
            </UserArea>
         </ItemMasterHeader>
      </ItemMaster>
   </DataArea>
</SyncItemMaster>

any suggestions? i am not an expert on xslt i tried several option using apply-template but for some reason didn't work at all thanks for your help

zanza67
  • 223
  • 4
  • 20

1 Answers1

0

Would this work for you:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:variable name="new-property">
    <Property>
        <NameValue>
            <xsl:text>/Item_Image[@Item_Name = "</xsl:text>
            <xsl:value-of select="//ID"/>
            <xsl:text>/"]</xsl:text>
        </NameValue>
    </Property> 
</xsl:variable>

<xsl:template match="UserArea">
    <xsl:copy>
        <xsl:copy-of select="$new-property"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ItemMasterHeader[not(UserArea)]">
    <xsl:copy>
        <xsl:apply-templates/>
        <UserArea>
            <xsl:copy-of select="$new-property"/>
        </UserArea>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51