xsl-list
[Top] [All Lists]

Re: [xsl] Need XSLT help Please

2016-07-28 23:21:07
What you are looking to do can be prescribed as:

1. When you see a <PS> element, include a copy of the <ANUMBER>
element that is its ancestor.

2. Otherwise, keep everything the same.

Let's do number 2 first: the easy way to do this in xslt3 is with this
instruction:

<xsl:mode on-no-match="shallow-copy"/>

This says "unless I tell you otherwise, just make a copy of everything
you see." (Technically it says "anytime you hit a node that has no
prescribed instruction, copy the node and then process each of its
elements inside the copy you made.")

To do number 1 you want something like this:

<xsl:template match="PS">
    <xsl:copy>
<xsl:copy-of select="@*"/>
        <xsl:copy-of select="ancestor::ANUMBER[1]"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

This says "When you hit a PS mode, make a copy (including any
attributes), then embed a copy of the nearest ancestor ANUMBER mode
(the "[1]" says "the first one you see as you move up the chain of
ancestors).
Then the <xsl:apply-templates/> instruction tells the program to move
on processing all the children of the <PS> element.

So altogether I think this should work:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    exclude-result-prefixes="xs"
    version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="PS">
    <xsl:copy>
<xsl:copy-of select="@*"/>
        <xsl:copy-of select="ancestor::ANUMBER"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>


On Thu, Jul 28, 2016 at 7:59 PM, Rahul Singh
rahulsinghindia15(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>
wrote:

Hi,

I am Begineer not much experience in XSL so that i have posted that question. 
I had tried lot but get fail. I need XSLT code for that sample input output:

I tried much but fail. My question is put ANUMBER from AC with corresponding 
PS.


Sample Input:

<?xml version="1.0" encoding="UTF-8"?>
<ACS>
   <AC>
      <ANUMBER>55555</ANUMBER>
      <PSS>
         <PS>
            <S>NEF</S>
            <C>63872R798</C>
         </PS>
      </PSS>
   </AC>
   <AC>
      <ANUMBER>330448</ANUMBER>
      <PSS>
         <PS>
            <S/>
            <C>1060150</C>
         </PS>
         <PS>
            <S/>
            <C>1260150</C>
         </PS>
      </PSS>
   </AC>
</ACS>




Sample OutPut:



<?xml version="1.0" encoding="UTF-8"?>
<ACS>
   <AC>
      <ANUMBER>55555</ANUMBER>
      <PSS>
         <PS>
            <ANUMBER>55555</ANUMBER>
            <S>NEF</S>
            <C>63872R798</C>
         </PS>
      </PSS>
   </AC>
   <AC>
      <ANUMBER>330448</ANUMBER>
      <PSS>
         <PS>
            <ANUMBER>330448</ANUMBER>
            <S/>
            <C>1060150</C>
         </PS>
         <PS>
            <ANUMBER>330448</ANUMBER>
            <S/>
            <C>1260150</C>
         </PS>
      </PSS>
   </AC>
</ACS>

XSL-List info and archive
EasyUnsubscribe (by email)




-- 

"A false conclusion, once arrived at and widely accepted is not
dislodged easily, and the less it is understood, the more tenaciously
it is held." - Cantor's Law of Preservation of Ignorance.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>
  • [xsl] Need XSLT help Please, Rahul Singh rahulsinghindia15(_at_)gmail(_dot_)com
    • Re: [xsl] Need XSLT help Please, David Rudel fwqhgads(_at_)gmail(_dot_)com <=