XSL: Copy original XML but make modifications to attribute and values

I have the following input xml and I want to be able to build a new <View> by keeping it's original contents but also copy the <Payments> from <Web> and prefix <Amount> with negative sign. And also copy the ServRef from original <View> where the Code and Number are equal but replace the @Doc with <Web>.


INPUT:
<A>
      <View>
            <PNRIdentification>
                  <PaymentInfo>
                        <PaymentDetails>
                              <Payments>
                                    <Amount>1575</Amount>
                                    <Reference>0438</Reference>
                                    <Type Type="CC">8902</Type>
                              </Payments>
                        </PaymentDetails>
                        <ServDet>
                              <ServRef Key="333" Doc="8380612270438">
                                    <FlightReferences>
                                          <Code>AS</Code>
                                          <Number>700</Number>
                                    </FlightReferences>
                                    <ServicePrice Total="1680">
                                          <BasePrice Amount="1500"/>
                                          <Taxes Amount="180">
                                                <Tax Amount="180">
                                                      <Nature>CA</Nature>
                                                </Tax>
                                          </Taxes>
                                    </ServicePrice>
                              </ServRef>
                        </ServDet>
                  </PaymentInfo>
            </PNRIdentification>
            <EmailAddress>
                  <Email>john@gmail.com</Email>
            </EmailAddress>
      </View>
      <Web>
            <Payments>
                  <Amount>1575</Amount>
                  <Reference>1908</Reference>
                  <Type Type="CR">8902</Type>
            </Payments>
            <ServRef Key="" Doc="8380491211908">
                  <FlightReferences>
                        <Code>AS</Code>
                        <Number>700</Number>
                  </FlightReferences>
            </ServRef>
      </Web>
</A>


OUTPUT:
<View>
      <PNRIdentification>
            <PaymentInfo>
                  <PaymentDetails>
                        <Payments>
                              <Amount>1575</Amount>
                              <Reference>0438</Reference>
                              <Type Type="CC">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>-1575</Amount>
                              <Reference>1908</Reference>
                              <Type Type="CR">8902</Type>
                        </Payments>
                  </PaymentDetails>
                  <ServDet>
                        <ServRef Key="333" Doc="8380612270438">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211908">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                  </ServDet>
            </PaymentInfo>
      </PNRIdentification>
      <EmailAddress>
            <Email>john@gmail.com</Email>
      </EmailAddress>
</View>
badtz7229Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
For a starter you need an identity copy template

    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
   
that will copy all

and now you can specialise some processing by adding more specific templates
0
Geert BormansInformation ArchitectCommented:
Now you need to add these two templates to only keep the View and pull in the Web Payment

    <xsl:template match="A">
             <xsl:apply-templates select="View"/>
    </xsl:template>
   
    <xsl:template match="PaymentDetails">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
            <xsl:apply-templates select="ancestor::View/following-sibling::Web/Payments"/>
        </xsl:copy>
    </xsl:template>
0
Geert BormansInformation ArchitectCommented:
And finally add this one to make the web payment negative

    <xsl:template match="Web/Payments/Amount">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:text>-</xsl:text>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

badtz7229Author Commented:
That worked excellent but the ServRef from original <View> where the Code and Number are equal but replace the @Doc with <Web> didn't come through.
0
Geert BormansInformation ArchitectCommented:
the ServRef template was not implemented because I saw no relation between the explanation and the expected output
Maybe this is what you need

    <xsl:template match="ServRef">
        <xsl:variable name="code" select="FlightReferences/Code"></xsl:variable>
        <xsl:variable name="number" select="FlightReferences/Number"></xsl:variable>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
        <xsl:apply-templates select="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number]"/>
    </xsl:template>
0
badtz7229Author Commented:
I want the View ServRef to be duplicated over when the Code and Number match what is found in <Web>. But replace the Doc with what is found in <Web>
So.

<ServDet>
      <ServRef Key="333" Doc="8380612270438">
            <FlightReferences>
                  <Code>AS</Code>
                  <Number>700</Number>
            </FlightReferences>
            <ServicePrice Total="1680">
                  <BasePrice Amount="1500"/>
                  <Taxes Amount="180">
                        <Tax Amount="180">
                              <Nature>CA</Nature>
                        </Tax>
                  </Taxes>
            </ServicePrice>
      </ServRef>
      <ServRef Key="333" Doc="8380491211908">
            <FlightReferences>
                  <Code>AS</Code>
                  <Number>700</Number>
            </FlightReferences>
            <ServicePrice Total="1680">
                  <BasePrice Amount="1500"/>
                  <Taxes Amount="180">
                        <Tax Amount="180">
                              <Nature>CA</Nature>
                        </Tax>
                  </Taxes>
            </ServicePrice>
      </ServRef>
</ServDet>
0
Geert BormansInformation ArchitectCommented:
I see, here is the template for the ref again

    <xsl:template match="ServRef">
        <xsl:variable name="code" select="FlightReferences/Code"></xsl:variable>
        <xsl:variable name="number" select="FlightReferences/Number"></xsl:variable>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
        <xsl:if test="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number]">
            <xsl:copy>
                <xsl:copy-of select="@*[not(name() = 'Doc')]"/>
                <xsl:copy-of select="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number]/@Doc"/>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>
0
badtz7229Author Commented:
It all worked successfully. thank you.

I'm trying to understand the current node location and how your code was able to achieve desired results.
For instance, in xsl:template match="PaymentDetails"
 
How does it know to copy the Web/Payments and paste it precisely right after the original Payments?

And in template match="Web/Payments/Amount" how does it know which Payments node it should prefix with a "-" ?
0
Geert BormansInformation ArchitectCommented:
In order to understand this, you need to understand the identity template mechanism

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

For a node() this one
- copies the tag using xsl:copy
- copies the attributes using xsl:copy-of select=@*
- and processes the content by pushing all the childnodes to the templates using apply-templates... these child nodes will be picked up by the most specific matching template

If this identity template is the only template, a nested processing of the entire document will take place, on each level, copying what needs to be copied and processing the content in the next level down... leading to an exact copy of the input to the output (statement correct up to a certain level)

As soon as you start adding specific templates you can "specialise" the process of a certain node

If you have this

    <xsl:template match="PaymentDetails">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
            <xsl:apply-templates select="ancestor::View/following-sibling::Web/Payments"/>
        </xsl:copy>
    </xsl:template>

this template will deal with the PaymentDetails element since for this one node
match="PaymentDetails" is more specific than match="node()"

As you can see, there are two apply-templates, one processing the direct chilren, and another one processing the web payments

This means that at that point in copying the document, the web Payments will be added, each of them being copied through the same identity template mechanism

except this one ...
<xsl:template match="Web/Payments/Amount">
which will copy the web Amount node... but puts a "-" in the actual amount
0
badtz7229Author Commented:
and how do you read  
      <xsl:apply-templates select="ancestor::View/following-sibling::Web/Payments"/>
in  <xsl:template match="PaymentDetails">
?

does this mean go to the root node , VIew and then go to the Payments and copy its contents?
0
Geert BormansInformation ArchitectCommented:
go to the ancestor View, take the sibling Web and "process" its payments
process is important, not copy, it could be copy, depends on the active templates at the lower level
It will not just be copy, because somewhere down there it will hit the amount template that adds the "-"
0
badtz7229Author Commented:
How do I do such that if the A/Web/ServRef and A/View/PNRIdentification/PaymentInfo/ServDet contain an <ElementNumber>
then do same action of copying the Web/ServRef not only where the Code and Number are the same but where the ElementNumber are same as well.

For example:

INPUT
<A>
      <View>
            <PNRIdentification>
                  <PaymentInfo>
                        <PaymentDetails>
                              <Payments>
                                    <Amount>1575</Amount>
                                    <Reference>0438</Reference>
                                    <Type Type="CC">8902</Type>
                              </Payments>
                        </PaymentDetails>
                        <ServDet>
                              <ElementNumber>1</ElementNumber>                                                
                              <ServRef Key="333" Doc="8380612270438">
                                    <FlightReferences>
                                          <Code>AS</Code>
                                          <Number>700</Number>
                                    </FlightReferences>
                                    <ServicePrice Total="1680">
                                          <BasePrice Amount="1500"/>
                                          <Taxes Amount="180">
                                                <Tax Amount="180">
                                                      <Nature>CA</Nature>
                                                </Tax>
                                          </Taxes>
                                    </ServicePrice>
                              </ServRef>
                        </ServDet>
                        <ServDet>
                              <ElementNumber>2</ElementNumber>                              
                              <ServRef Key="333" Doc="8380612270439">
                                                      <FlightReferences>
                                          <Code>AS</Code>
                                          <Number>700</Number>
                                    </FlightReferences>
                                    <ServicePrice Total="1680">
                                          <BasePrice Amount="1500"/>
                                          <Taxes Amount="180">
                                                <Tax Amount="180">
                                                      <Nature>CA</Nature>
                                                </Tax>
                                          </Taxes>
                                    </ServicePrice>
                              </ServRef>
                        </ServDet>                        
                  </PaymentInfo>
            </PNRIdentification>
            <EmailAddress>
                  <Email>john@gmail.com</Email>
            </EmailAddress>
      </View>
      <Web>
            <Payments>
                  <Amount>1575</Amount>
                  <Reference>1908</Reference>
                  <Type Type="CR">8902</Type>
            </Payments>
            <ServRef Key="" Doc="8380491211908">
                  <ElementNumber>1</ElementNumber>      
                  <FlightReferences>
                        <Code>AS</Code>
                        <Number>700</Number>
                  </FlightReferences>
            </ServRef>
                        <Payments>
                  <Amount>1575</Amount>
                  <Reference>1909</Reference>
                  <Type Type="CR">8902</Type>
            </Payments>
            <ServRef Key="" Doc="8380491211909">
                  <ElementNumber>2</ElementNumber>      
                  <FlightReferences>
                        <Code>AS</Code>
                        <Number>700</Number>
                  </FlightReferences>
            </ServRef>
      </Web>
</A>


OUTPUT
<View>
      <PNRIdentification>
            <PaymentInfo>
                  <PaymentDetails>
                        <Payments>
                              <Amount>1575</Amount>
                              <Reference>0438</Reference>
                              <Type Type="CC">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>-1575</Amount>
                              <Reference>1908</Reference>
                              <Type Type="CR">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>-1575</Amount>
                              <Reference>1909</Reference>
                              <Type Type="CR">8902</Type>
                        </Payments>
                  </PaymentDetails>
                  <ServDet>
                        <ElementNumber>1</ElementNumber>
                        <ServRef Key="333" Doc="8380612270438">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211908">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                  </ServDet>
                  <ServDet>
                        <ElementNumber>2</ElementNumber>
                        <ServRef Key="333" Doc="8380612270439">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211909">
                              <ElementNumber>2</ElementNumber>
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                  </ServDet>
            </PaymentInfo>
      </PNRIdentification>
      <EmailAddress>
            <Email>john@gmail.com</Email>
      </EmailAddress>
</View>
0
Geert BormansInformation ArchitectCommented:
So, there either is not an ElementNumber, but if there is one it needs to match?

Change this
   <xsl:template match="ServRef">
        <xsl:variable name="code" select="FlightReferences/Code"></xsl:variable>
        <xsl:variable name="number" select="FlightReferences/Number"></xsl:variable>
<xsl:if test="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number]">


into

   <xsl:template match="ServRef">
        <xsl:variable name="code" select="FlightReferences/Code"></xsl:variable>
        <xsl:variable name="number" select="FlightReferences/Number"></xsl:variable>
        <xsl:variable name="elemnr" select="ElementNumber"></xsl:variable>
<xsl:if test="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number][not(normalize-space($elemnr)) or ElementNumber = $elemnr]">
0
Geert BormansInformation ArchitectCommented:
And of course add the extra test to the @Doc constructor

<xsl:copy-of select="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number][not(normalize-space($elemnr)) or ElementNumber = $elemnr]/@Doc"/>
0
badtz7229Author Commented:
I have the following
  <xsl:template match="ServRef">
    <xsl:variable name="code" select="FlightReferences/Code"></xsl:variable>
    <xsl:variable name="number" select="FlightReferences/Number"></xsl:variable>
    <xsl:variable name="elemnr" select="ElementNumber"></xsl:variable>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>    
    <xsl:if test="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number][not(normalize-space($elemnr)) or ElementNumber = $elemnr]">
    <xsl:copy>
      <xsl:copy-of select="@*[not(name() = 'Doc')]"/>
      <xsl:copy-of select="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number]
      [not(normalize-space($elemnr)) or ElementNumber = $elemnr]/@Doc"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
    </xsl:if>
  </xsl:template>
 
  But this is returning the last ServRef found for both ElementNumber
  <View>
      <PNRIdentification>
            <PaymentInfo>
                  <PaymentDetails>
                        <Payments>
                              <Amount>1575</Amount>
                              <Reference>0438</Reference>
                              <Type Type="CC">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>-1575</Amount>
                              <Reference>1908</Reference>
                              <Type Type="CR">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>-1575</Amount>
                              <Reference>1909</Reference>
                              <Type Type="CR">8902</Type>
                        </Payments>
                  </PaymentDetails>
                  <ServDet>
                        <ElementNumber>1</ElementNumber>
                        <ServRef Key="333" Doc="8380612270438">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211909">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                  </ServDet>
                  <ServDet>
                        <ElementNumber>2</ElementNumber>
                        <ServRef Key="333" Doc="8380612270439">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211909">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                  </ServDet>
            </PaymentInfo>
      </PNRIdentification>
      <EmailAddress>
            <Email>john@gmail.com</Email>
      </EmailAddress>
</View>
0
Geert BormansInformation ArchitectCommented:
Can you please post your full stylesheet since there seem to be some other structural changes that happened. I have a deadline by tomorrow morning, so I might not be able to look into it immedeatly
0
badtz7229Author Commented:
0
Geert BormansInformation ArchitectCommented:
Element Number was on a different level than I expected

  <xsl:template match="ServRef">
    <xsl:variable name="code" select="FlightReferences/Code"></xsl:variable>
    <xsl:variable name="number" select="FlightReferences/Number"></xsl:variable>
    <xsl:variable name="elemnr" select="ElementNumber"></xsl:variable>
    <xsl:copy>

the variable elemnr should be like this
    <xsl:variable name="elemnr" select="../ElementNumber"></xsl:variable>
0
badtz7229Author Commented:
yes. that worked.
0
badtz7229Author Commented:
I'm passing this input
<A>
      <View>
            <PNRIdentification>
                  <PaymentInfo>
                        <PaymentDetails>
                              <Payments>
                                    <Amount>1575</Amount>
                                    <Reference>8380612270585</Reference>
                                    <Type Type="CC">8902</Type>
                              </Payments>
                              <Payments>
                                    <Amount>1575</Amount>
                                    <Reference>8380612270584</Reference>
                                    <Type Type="CC">8902</Type>
                              </Payments>
                              <Payments>
                                    <Amount>1575</Amount>
                                    <Reference>8380491211936</Reference>
                                    <Type Type="CR">8902</Type>
                              </Payments>
                        </PaymentDetails>
                        <ServDet>
                              <ElementNumber>1</ElementNumber>
                              <ServRef Key="333" Doc="8380612270584">
                                    <FlightReferences>
                                          <Code>AS</Code>
                                          <Number>700</Number>
                                    </FlightReferences>
                                    <ServicePrice Total="1680">
                                          <BasePrice Amount="1500"/>
                                          <Taxes Amount="180">
                                                <Tax Amount="180">
                                                      <Nature>CA</Nature>
                                                </Tax>
                                          </Taxes>
                                    </ServicePrice>
                              </ServRef>
                              <ServRef Key="333" Doc="8380491211936">
                                    <FlightReferences>
                                          <Code>AS</Code>
                                          <Number>700</Number>
                                    </FlightReferences>
                                    <ServicePrice Total="1680">
                                          <BasePrice Amount="1500"/>
                                          <Taxes Amount="180">
                                                <Tax Amount="180">
                                                      <Nature>CA</Nature>
                                                </Tax>
                                          </Taxes>
                                    </ServicePrice>
                              </ServRef>
                              <ServRef Key="333" Doc="8380612270585">
                                    <FlightReferences>
                                          <Code>AS</Code>
                                          <Number>700</Number>
                                    </FlightReferences>
                                    <ServicePrice Total="1680">
                                          <BasePrice Amount="1500"/>
                                          <Taxes Amount="180">
                                                <Tax Amount="180">
                                                      <Nature>CA</Nature>
                                                </Tax>
                                          </Taxes>
                                    </ServicePrice>
                              </ServRef>
                        </ServDet>
                  </PaymentInfo>
            </PNRIdentification>
            <EmailAddress>
                  <Email>john@gmail.com</Email>
            </EmailAddress>
      </View>
      <Web>
            <Payments>
                  <Amount>1575</Amount>
                  <Reference>8380491211908</Reference>
                  <Type Type="CR">8902</Type>
            </Payments>
            <ServRef Key="" Doc="8380491211908">
                  <ElementNumber>1</ElementNumber>
                  <FlightReferences>
                        <Code>AS</Code>
                        <Number>700</Number>
                  </FlightReferences>
            </ServRef>
      </Web>
</A>

But the output seems to copy over the Web/ServRef for each A/View/PNRIdentification/PaymentInfo/ServRef that already existed. So I end up with duplicate entries when it should only be one additional entry.



Output:
<View>
      <PNRIdentification>
            <PaymentInfo>
                  <PaymentDetails>
                        <Payments>
                              <Amount>1575</Amount>
                              <Reference>8380612270585</Reference>
                              <Type Type="CC">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>1575</Amount>
                              <Reference>8380612270584</Reference>
                              <Type Type="CC">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>1575</Amount>
                              <Reference>8380491211936</Reference>
                              <Type Type="CR">8902</Type>
                        </Payments>
                        <Payments>
                              <Amount>-1575</Amount>
                              <Reference>8380491211908</Reference>
                              <Type Type="CR">8902</Type>
                        </Payments>
                  </PaymentDetails>
                  <ServDet>
                        <ElementNumber>1</ElementNumber>
                        <ServRef Key="333" Doc="8380612270584">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211908">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211936">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211908">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380612270585">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                        <ServRef Key="333" Doc="8380491211908">
                              <FlightReferences>
                                    <Code>AS</Code>
                                    <Number>700</Number>
                              </FlightReferences>
                              <ServicePrice Total="1680">
                                    <BasePrice Amount="1500"/>
                                    <Taxes Amount="180">
                                          <Tax Amount="180">
                                                <Nature>CA</Nature>
                                          </Tax>
                                    </Taxes>
                              </ServicePrice>
                        </ServRef>
                  </ServDet>
            </PaymentInfo>
      </PNRIdentification>
      <EmailAddress>
            <Email>john@gmail.com</Email>
      </EmailAddress>
</View>
0
Geert BormansInformation ArchitectCommented:
I hope you are not making this the eternal question...

Here is the condition to duplicate a ServRef
    <xsl:if test="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number][not(normalize-space($elemnr)) or ElementNumber = $elemnr]">

It basically says, if there is a web ServRef whit this particular conditions, duplicate the CURRENT ServRef, but with the web servref @Doc

It seems to me that that is what you asked (and got)

Given that I am duplicating a certain servref with particular conditions... you seem to imply in your head that I should not do the duplication if an equal servref happened earlier...
I can add a condition for an earlier ServRef... but when do you consider them "equal"?
If the Key is equal?
If they are in the same ServDet?
If code and number are equal?
I need to know the decision point that triggers the "do not duplicate"
0
badtz7229Author Commented:
"if there is a web ServRef whit this particular conditions, duplicate the CURRENT ServRef, but with the web servref @Doc" still holds true but to only copy it once.

So yes, if after the new entry it tries to insert again in the same ServDet "do no duplicate".
0
Geert BormansInformation ArchitectCommented:
That would then be something like this

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



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

  <xsl:template match="A">
    <xsl:apply-templates select="View"/>
  </xsl:template>

  <xsl:template match="PaymentDetails">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()"/>
      <xsl:apply-templates select="ancestor::View/following-sibling::Web/Payments"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="Web/Payments/Amount">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:text>-</xsl:text>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ServRef">
    <xsl:variable name="code" select="FlightReferences/Code"></xsl:variable>
    <xsl:variable name="number" select="FlightReferences/Number"></xsl:variable>
    <xsl:variable name="elemnr" select="../ElementNumber"></xsl:variable>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>    
    <xsl:if test="
      ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number][not(normalize-space($elemnr)) or ElementNumber = $elemnr]
      and not(preceding-sibling::ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number])
      ">
    <xsl:copy>
      <xsl:copy-of select="@*[not(name() = 'Doc')]"/>
      <xsl:copy-of select="ancestor::View/following-sibling::Web/ServRef[FlightReferences/Code = $code][FlightReferences/Number = $number]
      [not(normalize-space($elemnr)) or ElementNumber = $elemnr]/@Doc"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
    </xsl:if>
  </xsl:template>
      
</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
badtz7229Author Commented:
yes that avoided the duplicate entries. thank you.
0
Geert BormansInformation ArchitectCommented:
welcome
0
badtz7229Author Commented:
thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.