Results 1 to 7 of 7

Thread: [RESOLVED] Applying XSL Transformation!

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved [RESOLVED] Applying XSL Transformation!

    I have a well formed XML that I need to modify.

    My input file looks like this.
    Code:
    <?xml version="1.0"?>
    <Root>
      <VOBaseCollection>
        <VOUploadPackage>
          <VOPartyContainer>
    		...blah
          </VOPartyContainer>
        </VOUploadPackage>
    <VOPartyContainer>
    		...blah
          </VOPartyContainer>
        <VOUploadPackage>
          ...blah
    	</VOUploadPackage>
      </VOBaseCollection>
    </Root>
    I used a XSL transformation to get my desired output, but it came with a slightly different output file.
    This is my XSL transformation.
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
    
    <xsl:template match="/">
          <Root BatchUploadType="Party">
             <VOUploadBatch>
                <UploadPackages>
                   <VOBaseCollection>
                      <VOUploadPackage>
                            <xsl:apply-templates select="Root/VOBaseCollection/VOUploadPackage/VOPartyContainer" />
                      </VOUploadPackage>
                   </VOBaseCollection>
                </UploadPackages>
             </VOUploadBatch>
          </Root>
       </xsl:template>
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()" />
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>

    My desired output should like this.
    Code:
    <?xml version="1.0"?>
    <Root BatchUploadType="Party">
      <VOUploadBatch>
        <UploadPackages>
          <VOBaseCollection>
            <VOUploadPackage>
              <VOPartyContainer>
    blah
    </VOPartyContainer>
            </VOUploadPackage>
            <VOUploadPackage>
              <VOPartyContainer>
    blah
    </VOPartyContainer>
            </VOUploadPackage>
          </VOBaseCollection>
        </UploadPackages>
      </VOUploadBatch>
    </Root>

    However after applying the transformation, it looks like this.
    Code:
    <?xml version="1.0"?>
    <Root BatchUploadType="Party">
      <VOUploadBatch>
        <UploadPackages>
          <VOBaseCollection>
            <VOUploadPackage>
              <VOPartyContainer>
              </VOPartyContainer>
             <VOPartyContainer>
              </VOPartyContainer>
           </VOUploadPackage>
          </VOBaseCollection>
        </UploadPackages>
      </VOUploadBatch>
    </Root>
    What changes do I need to make in my XSL to get it transformed correctly?
    I do not wish to lose the <VOUploadBatch> tag
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Applying XSL Transformation!

    I'm not great with XSL, but could you use something like this instead...?

    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
    
    <xsl:template match="/">
      <Root BatchUploadType="Party">
         <VOUploadBatch>
            <UploadPackages>
               <VOBaseCollection>
                  <xsl:for-each select="Root/VOBaseCollection/*">
                    <VOUploadPackage>
                      <VOPartyContainer>
                        <xsl:value-of select="."/>
                      </VOPartyContainer>
                    </VOUploadPackage>
                  </xsl:for-each>                  
               </VOBaseCollection>
            </UploadPackages>
         </VOUploadBatch>
      </Root>
    </xsl:template>
    </xsl:stylesheet>
    Using your input XML, it outputs:
    Code:
    <Root BatchUploadType="Party">
      <VOUploadBatch>
        <UploadPackages>
          <VOBaseCollection>
            <VOUploadPackage>
              <VOPartyContainer>
              ...blah
              </VOPartyContainer>
            </VOUploadPackage>
            <VOUploadPackage>
              <VOPartyContainer>
              ...blah
              </VOPartyContainer>
            </VOUploadPackage>
            <VOUploadPackage>
              <VOPartyContainer>
              ...blah
              </VOPartyContainer>
            </VOUploadPackage>        
          </VOBaseCollection>
        </UploadPackages>
      </VOUploadBatch>
    </Root>

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Applying XSL Transformation!

    SambaNeko,
    Thank you for responding to my thread. I will have to try that out.
    I am n00b when it comes to all the XML stuff.

    I did a string concatenation to get the correct output. That is not only time consuming, but also dangerous.
    I will give this approach a shot.

    Thanks Again!
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Applying XSL Transformation!

    Using; <xsl:value-of select="." />

    You can also use; <xsl;values-of select="text()" />

    The xsl:copy recreates the element and all of its children.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Applying XSL Transformation!

    Is there some way to make the select = text() put in tags.
    Right now, the "blah" in my initial xml represents another xml structure. I do not wish to disturb this.
    If I use Samba's XSLT, the xml structure gets output as text (without the tags).
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Applying XSL Transformation!

    Use the transformation you had originally then.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Applying XSL Transformation!

    Apologies; I now notice the subtle difference between the two formats What you had originally is on the right track, however your original XPath selection is too specific. You appear to need a <VOUploadPackage> for each <VOPartyContainer> and VoPartyContainers can be a child element of VOUploadPackage or VOBaseCollection.

    With that in mind you need to us "//" to select any "VOpartyContainer" which is any where in the VOBaseCollection hierarchy then output the VOUploadPackage in a template for VOPartyContainers. You will also need to use xsl:copy-of to make a deep copy of the element instead of the "catch-all" template you originally had.

    See below;
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
    
    <xsl:template match="/">
          <Root BatchUploadType="Party">
             <VOUploadBatch>
                <UploadPackages>
                   <VOBaseCollection>
                            <xsl:apply-templates select="Root/VOBaseCollection//VOPartyContainer" />
                   </VOBaseCollection>
                </UploadPackages>
             </VOUploadBatch>
          </Root>
       </xsl:template>
    
       <xsl:template match="VOPartyContainer">
                      <VOUploadPackage>
                            <xsl:copy-of select="." />
                      </VOUploadPackage>
       </xsl:template>
    </xsl:stylesheet>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width