locked
XSLT for adding a Copied node to the XML based on a condition for a Value in XML node. RRS feed

  • Question

  • Hi , 

    i am trying to achieve the below results from the set of input  as below expected scenarios

    1.  Addition of Info Node
    For each Transaction 
    If Any Sales Have Info with value Type1 Then
    All Sales should be populated with Info Node same found for one Sales for that particular Transaction

    2. No Addition Just pass what was recieved

    If For each transaction 
    If no sales found with Info with value Type1 Then 
    Just pass the Transaction & sales nodes as it is they received. 

    Not able to check for any logic to check the logic that  how i can check the existence of node value for each sales and transaction separately. 

    Input XML

    INput

    Output (Expected) :
    Output

    Any suggestions will be appreciated.  

    Thursday, November 5, 2015 4:21 PM

Answers

  • How about this:

    <?xml version="1.0" encoding="utf-16"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://SchemaIN" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0">
      <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
      <xsl:template match="/">
        <xsl:apply-templates select="ns0:Main" />
      </xsl:template>
      <xsl:template match="ns0:Main">
        <ns0:Main>
          <xsl:for-each select="Transaction">
            <Transaction>
              <xsl:for-each select="Sales">
                <Sales>
                  <xsl:for-each select="Item">
                    <Item>
                      <A>
                        <xsl:value-of select="A" />
                      </A>
                      <B>
                        <xsl:value-of select="B" />
                      </B>
                    </Item>
                  </xsl:for-each>
                  <xsl:choose>
                    <xsl:when test="Info">
                      <Info>
                        <Type>
                          <xsl:value-of select="Info/Type" />
                        </Type>
                        <Data>
                          <xsl:value-of select="Info/Data" />
                        </Data>
                      </Info>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:if test="../Sales/Info[Type = 'Type1']">
                        <Info>
                          <Type>
                            <xsl:value-of select="../Sales/Info[Type = 'Type1']/Type" />
                          </Type>
                          <Data>
                            <xsl:value-of select="../Sales/Info[Type = 'Type1']/Data" />
                          </Data>
                        </Info>
                      </xsl:if>
                    </xsl:otherwise>
                  </xsl:choose>
                </Sales>
              </xsl:for-each>
            </Transaction>
          </xsl:for-each>
        </ns0:Main>
      </xsl:template>
    </xsl:stylesheet>

    Morten la Cour

    • Marked as answer by Angie Xu Monday, November 16, 2015 2:16 AM
    Thursday, November 5, 2015 7:58 PM