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


    Output (Expected) :

    Any suggestions will be appreciated.  

    Thursday, November 5, 2015 4:21 PM


  • 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 match="ns0:Main">
          <xsl:for-each select="Transaction">
              <xsl:for-each select="Sales">
                  <xsl:for-each select="Item">
                        <xsl:value-of select="A" />
                        <xsl:value-of select="B" />
                    <xsl:when test="Info">
                          <xsl:value-of select="Info/Type" />
                          <xsl:value-of select="Info/Data" />
                      <xsl:if test="../Sales/Info[Type = 'Type1']">
                            <xsl:value-of select="../Sales/Info[Type = 'Type1']/Type" />
                            <xsl:value-of select="../Sales/Info[Type = 'Type1']/Data" />

    Morten la Cour

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