Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Need help with XSLT

Need help with XSLT

Scheduled Pinned Locked Moved XML / XSL
xmlhelpdatabasesql-serversysadmin
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alsvha
    wrote on last edited by
    #1

    I have a problem I'm looking to solve with XSLT, but I'm a bit stumped at how to implement the syntax. I basically have a XML file with a structure narrowed down to this: <ITEM> <NAMEVALUE>    <NAME>From1</NAME>    <VALUE>From1 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>From2</NAME>    <VALUE>From2 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>Price1</NAME>    <VALUE>Price1 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>Price2</NAME>    <VALUE>Price2 Value</VALUE> </NAMEVALUE> . . . . </ITEM> (I can't change the format of the delivered file). I would like to transform it into something equivalent to: <Prices> <Price from="From1 Value">Price1 Value</Price> <Price from="From2 Value">Price2 Value</Price> </Prices> And I'm quite bugged down in it. I can most likely fix it using scripts in my XML file and some temporary variables, but I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.

    --------------------------- Blogging about SQL Server and related topics.

    L S 2 Replies Last reply
    0
    • A Alsvha

      I have a problem I'm looking to solve with XSLT, but I'm a bit stumped at how to implement the syntax. I basically have a XML file with a structure narrowed down to this: <ITEM> <NAMEVALUE>    <NAME>From1</NAME>    <VALUE>From1 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>From2</NAME>    <VALUE>From2 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>Price1</NAME>    <VALUE>Price1 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>Price2</NAME>    <VALUE>Price2 Value</VALUE> </NAMEVALUE> . . . . </ITEM> (I can't change the format of the delivered file). I would like to transform it into something equivalent to: <Prices> <Price from="From1 Value">Price1 Value</Price> <Price from="From2 Value">Price2 Value</Price> </Prices> And I'm quite bugged down in it. I can most likely fix it using scripts in my XML file and some temporary variables, but I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.

      --------------------------- Blogging about SQL Server and related topics.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Alsvha wrote:

      I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.

      I don't understand how "From1 Value" is related to "Price1 Value" in the XML source?

      A 1 Reply Last reply
      0
      • L led mike

        Alsvha wrote:

        I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.

        I don't understand how "From1 Value" is related to "Price1 Value" in the XML source?

        A Offline
        A Offline
        Alsvha
        wrote on last edited by
        #3

        They're related due to the name From1 is connected to Price1 and From2 to Price2 and so forth. It is an unfortunate format, but it is what I have to work with :)

        --------------------------- Blogging about SQL Server and related topics.

        L 1 Reply Last reply
        0
        • A Alsvha

          I have a problem I'm looking to solve with XSLT, but I'm a bit stumped at how to implement the syntax. I basically have a XML file with a structure narrowed down to this: <ITEM> <NAMEVALUE>    <NAME>From1</NAME>    <VALUE>From1 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>From2</NAME>    <VALUE>From2 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>Price1</NAME>    <VALUE>Price1 Value</VALUE> </NAMEVALUE> <NAMEVALUE>    <NAME>Price2</NAME>    <VALUE>Price2 Value</VALUE> </NAMEVALUE> . . . . </ITEM> (I can't change the format of the delivered file). I would like to transform it into something equivalent to: <Prices> <Price from="From1 Value">Price1 Value</Price> <Price from="From2 Value">Price2 Value</Price> </Prices> And I'm quite bugged down in it. I can most likely fix it using scripts in my XML file and some temporary variables, but I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.

          --------------------------- Blogging about SQL Server and related topics.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          The following works (i.e. I've tested it) in Microsoft XSLT - but it. It creates a key containing all 'Price' NAMEVALUE nodes, indexed on the text after 'Price' in the NAMEVALUE/NAME node. This is used in the 'From' NAMEVALUE node template, to establish the linkage between 'From' and 'Price' nodes.

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

          <xsl:key name="prices" match="/ITEM/NAMEVALUE[starts-with(NAME/text(), 'Price')]" use="substring-after(NAME/text(), 'Price')"/>

          <xsl:template match="/">
          <prices>
          <xsl:apply-templates select="//NAMEVALUE"/>
          </prices>
          </xsl:template>

          <xsl:template match="NAMEVALUE[starts-with(NAME/text(), 'From')]">
          <Price from="{VALUE/text()}">
          <xsl:value-of select="key('prices', substring-after(NAME/text(), 'From'))/VALUE"/>
          </Price>
          </xsl:template>

          <xsl:template match="NAMEVALUE"/>

          </xsl:stylesheet>

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          L A 2 Replies Last reply
          0
          • S Stuart Dootson

            The following works (i.e. I've tested it) in Microsoft XSLT - but it. It creates a key containing all 'Price' NAMEVALUE nodes, indexed on the text after 'Price' in the NAMEVALUE/NAME node. This is used in the 'From' NAMEVALUE node template, to establish the linkage between 'From' and 'Price' nodes.

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

            <xsl:key name="prices" match="/ITEM/NAMEVALUE[starts-with(NAME/text(), 'Price')]" use="substring-after(NAME/text(), 'Price')"/>

            <xsl:template match="/">
            <prices>
            <xsl:apply-templates select="//NAMEVALUE"/>
            </prices>
            </xsl:template>

            <xsl:template match="NAMEVALUE[starts-with(NAME/text(), 'From')]">
            <Price from="{VALUE/text()}">
            <xsl:value-of select="key('prices', substring-after(NAME/text(), 'From'))/VALUE"/>
            </Price>
            </xsl:template>

            <xsl:template match="NAMEVALUE"/>

            </xsl:stylesheet>

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            Now that's some nice XSLT! Great example of using key, get's my 5.

            1 Reply Last reply
            0
            • A Alsvha

              They're related due to the name From1 is connected to Price1 and From2 to Price2 and so forth. It is an unfortunate format, but it is what I have to work with :)

              --------------------------- Blogging about SQL Server and related topics.

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Alsvha wrote:

              It is an unfortunate format

              Yes. I can't help you exercise the demons from the people that created that XML nightmare, however Stuart has provided an example for you. :laugh: :laugh: exercise exorcise

              modified on Thursday, June 4, 2009 1:45 PM

              1 Reply Last reply
              0
              • S Stuart Dootson

                The following works (i.e. I've tested it) in Microsoft XSLT - but it. It creates a key containing all 'Price' NAMEVALUE nodes, indexed on the text after 'Price' in the NAMEVALUE/NAME node. This is used in the 'From' NAMEVALUE node template, to establish the linkage between 'From' and 'Price' nodes.

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

                <xsl:key name="prices" match="/ITEM/NAMEVALUE[starts-with(NAME/text(), 'Price')]" use="substring-after(NAME/text(), 'Price')"/>

                <xsl:template match="/">
                <prices>
                <xsl:apply-templates select="//NAMEVALUE"/>
                </prices>
                </xsl:template>

                <xsl:template match="NAMEVALUE[starts-with(NAME/text(), 'From')]">
                <Price from="{VALUE/text()}">
                <xsl:value-of select="key('prices', substring-after(NAME/text(), 'From'))/VALUE"/>
                </Price>
                </xsl:template>

                <xsl:template match="NAMEVALUE"/>

                </xsl:stylesheet>

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                A Offline
                A Offline
                Alsvha
                wrote on last edited by
                #7

                Thank you so much - I'll try to take a look at it.

                --------------------------- Blogging about SQL Server and related topics.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups