Search code examples
xsltxslt-1.0xslt-grouping

XSLT Mapping Transformation


Iam novice to XSLT mapping and looking for help to achieve desired output. I want to copy Input XML as is in to output. I have tried using xsl:copy-of select which is adding an empty xmls=”” attribute in to /Inventory/Action which is not acceptable by target. Also When I try xsl:for-each using below XSLT values for elements like AdAction , unit<4567 7896> and ID<12345 67890> QU<200 100> are repeating for each iteration .

Input XML.

<?xml version="1.0" encoding="UTF-8"?>
<Body>
  <Inventory>
     <Action MessageType="hello" Type="hi" Action="hand"      InventoryState="avail">
        <DateTime/>
        <Ad Action="Set">
           <Position>
              <Unit TypeCode="abc">4567</Unit>
              <Item>
                 <ID>12345</ID>
              </Item>
           </Position>
           <Qu>200</Qu>
        </Ad>
     </Action>
           <Action MessageType="hello" Type="hi" Action="hand" InventoryState="avail">
        <DateTime/>
        <Ad Action="Set">
           <Position>
              <Unit TypeCode="abc">7896</Unit>
              <Item>
                 <ID>67890</ID>
              </Item>
           </Position>
           <Qu>100</Qu>
        </Ad>
     </Action>
  </Inventory>

XSLT mapping I have used.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="xsl">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
    <Body>
        <xsl:for-each select="//Action">
            <Action>
                <xsl:attribute name="Type">hello</xsl:attribute>
                <xsl:attribute name="AType">hi</xsl:attribute>
                <xsl:attribute name="Action">hand</xsl:attribute>
                <xsl:attribute name="State">avail</xsl:attribute>
                <DateTime>DateTime</DateTime>
                <Ad>
                    <xsl:attribute name="Action"><xsl:value-of select="//Ad/@Action"/></xsl:attribute>
                    <Position>
                        <Unit>
                            <xsl:attribute name="TypeCode">Store</xsl:attribute>
                        </Unit>
                        <Item>
                            <ID>
                                <xsl:value-of select="//Action/Ad/Position/Item/ID"/>
                            </ID>
                        </Item>
                    </Position>
                    <Qu>
                        <xsl:value-of select="//Action/Ad/Qu"/>
                    </Qu>
                </Ad>
            </Action>
        </xsl:for-each>
    </Body>
</xsl:template>

Output:

<Body xmlns="http://www.w3.org/1999/XSL/Format">
<Action Type="hello" AType="hi" Action="hand" State="avail">
    <DateTime>DateTime</DateTime>
    <Ad Action="Set Set">
        <Position>
            <Unit TypeCode="Store"/>
            <Item>
                <ID>12345 67890</ID>
            </Item>
        </Position>
        <Qu>200 100</Qu>
    </Ad>
</Action>
<Action Type="hello" AType="hi" Action="hand" State="avail">
    <DateTime>DateTime</DateTime>
    <Ad Action="Set Set">
        <Position>
            <Unit TypeCode="Store"/>
            <Item>
                <ID>12345 67890</ID>
            </Item>
        </Position>
        <Qu>200 100</Qu>
    </Ad>
</Action>

Desired Output no repeating Values.

<Body xmlns="http://www.w3.org/1999/XSL/Format">
<Action Type="hello" AType="hi" Action="hand" State="avail">
    <DateTime>DateTime</DateTime>
    <Ad Action="Set">
        <Position>
            <Unit TypeCode="Store"/>
            <Item>
                <ID>12345</ID>
            </Item>
        </Position>
        <Qu>200</Qu>
    </Ad>
</Action>
<Action Type="hello" AType="hi" Action="hand" State="avail">
    <DateTime>DateTime</DateTime>
    <Ad Action="Set">
        <Position>
            <Unit TypeCode="Store"/>
            <Item>
                <ID>67890</ID>
            </Item>
        </Position>
        <Qu>100</Qu>
    </Ad>
</Action>


Solution

  • Change:

    <xsl:value-of select="//Action/Ad/Position/Item/ID"/>
    

    to:

    <xsl:value-of select="Ad/Position/Item/ID"/>
    

    What you have is an absolute path, starting from the root - and it will select every item ID in the entire document. What you need is a relative path, starting from the current Action.

    Similarly, change:

    <xsl:value-of select="//Action/Ad/Qu"/>
    

    to:

    <xsl:value-of select="Ad/Qu"/>