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. Maintain the order of the xml

Maintain the order of the xml

Scheduled Pinned Locked Moved XML / XSL
xmlhtmlwpfregexhelp
5 Posts 2 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.
  • Y Offline
    Y Offline
    Yariv
    wrote on last edited by
    #1

    Hi, I'm generating an xml dynamically and want to use xsl as a template (and therefore not generated dynamically). Here is an example of such xml: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="Template.xsl"?> <PeopleList order="1"> <item> <Name>John</Name> </item> <item> <Name>Doe</Name> </item> </PeopleList> <KidList order="2"> <item> <Name>Alex</Name> </item> <item> <Name>King</Name> </item> </KidList> Template.xsl looks like this: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <h2>My List</h2> <xsl:apply-templates select="KidList"/> <xsl:apply-templates select="PeopleList"/> </body> </html> </xsl:template> <xsl:template match="PeopleList"> <p> People List </p> </xsl:template> <xsl:template match="KidList"> <p> KidList </p> </xsl:template> </xsl:stylesheet> Result will be (the order was decided according xsl:apply-templates and not according the xml) :

    My List

    Kid List

    People List

    In the xml, PeopleList can come before KidList or after it and the order is matter - I want one xsl file that can display them in the right order. I added an "order" attribute but still don't know if it can help and how. Any suggestions?

    M 1 Reply Last reply
    0
    • Y Yariv

      Hi, I'm generating an xml dynamically and want to use xsl as a template (and therefore not generated dynamically). Here is an example of such xml: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="Template.xsl"?> <PeopleList order="1"> <item> <Name>John</Name> </item> <item> <Name>Doe</Name> </item> </PeopleList> <KidList order="2"> <item> <Name>Alex</Name> </item> <item> <Name>King</Name> </item> </KidList> Template.xsl looks like this: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <h2>My List</h2> <xsl:apply-templates select="KidList"/> <xsl:apply-templates select="PeopleList"/> </body> </html> </xsl:template> <xsl:template match="PeopleList"> <p> People List </p> </xsl:template> <xsl:template match="KidList"> <p> KidList </p> </xsl:template> </xsl:stylesheet> Result will be (the order was decided according xsl:apply-templates and not according the xml) :

      My List

      Kid List

      People List

      In the xml, PeopleList can come before KidList or after it and the order is matter - I want one xsl file that can display them in the right order. I added an "order" attribute but still don't know if it can help and how. Any suggestions?

      M Offline
      M Offline
      Mark J Miller
      wrote on last edited by
      #2

      Have you tried changing this: <h2>My List</h2> <xsl:apply-templates select="KidList"/> <xsl:apply-templates select="PeopleList"/> to this: <h2>My List</h2> <xsl:apply-templates select="PeopleList"/> <xsl:apply-templates select="KidList"/>

      Mark's blog: developMENTALmadness.blogspot.com Funniest variable name: lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher

      Y 1 Reply Last reply
      0
      • M Mark J Miller

        Have you tried changing this: <h2>My List</h2> <xsl:apply-templates select="KidList"/> <xsl:apply-templates select="PeopleList"/> to this: <h2>My List</h2> <xsl:apply-templates select="PeopleList"/> <xsl:apply-templates select="KidList"/>

        Mark's blog: developMENTALmadness.blogspot.com Funniest variable name: lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher

        Y Offline
        Y Offline
        Yariv
        wrote on last edited by
        #3

        Thanks for replying, but this is not the solution. The xml is built dynamically so sometimes KidList will be before PeopleList and sometimes not (I can't change the xsl so I need it to be generic). The solution is: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <xsl:apply-templates select="Lists"/> </body> </html> </xsl:template> <xsl:template match="Lists"> <h2>My List</h2> <xsl:for-each select="*"> <xsl:variable name="TemplateName" select="."/> <xsl:apply-templates select="$TemplateName"/> </xsl:for-each> </xsl:template> <xsl:template match="PeopleList"> <p> People List </p> </xsl:template> <xsl:template match="KidList"> <p> KidList </p> </xsl:template> </xsl:stylesheet>

        M 1 Reply Last reply
        0
        • Y Yariv

          Thanks for replying, but this is not the solution. The xml is built dynamically so sometimes KidList will be before PeopleList and sometimes not (I can't change the xsl so I need it to be generic). The solution is: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <xsl:apply-templates select="Lists"/> </body> </html> </xsl:template> <xsl:template match="Lists"> <h2>My List</h2> <xsl:for-each select="*"> <xsl:variable name="TemplateName" select="."/> <xsl:apply-templates select="$TemplateName"/> </xsl:for-each> </xsl:template> <xsl:template match="PeopleList"> <p> People List </p> </xsl:template> <xsl:template match="KidList"> <p> KidList </p> </xsl:template> </xsl:stylesheet>

          M Offline
          M Offline
          Mark J Miller
          wrote on last edited by
          #4

          If you can't control the order of KidList and PeopleList then the result your solution will always be the order of the xml document. If KidList is first in the Xml then it will be first in your output. If you need it to be more generic then you will need to do this: <xsl:for-each select="*"> <xsl:sort select="@order" data-type="number" /> <xsl:variable name="TemplateName" select="."/> <xsl:apply-templates select="$TemplateName"/> </xsl:for-each> That is assuming you are still using the "order" attribute in your xml that was in your first post. Otherwise you'll have to sort by the name of the element in descending order (PeopleList then KidList). <xsl:for-each select="*"> <xsl:sort select="." data-type="text" order="descending" /> <xsl:variable name="TemplateName" select="."/> <xsl:apply-templates select="$TemplateName"/> </xsl:for-each>

          Mark's blog: developMENTALmadness.blogspot.com Funniest variable name: lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher

          Y 1 Reply Last reply
          0
          • M Mark J Miller

            If you can't control the order of KidList and PeopleList then the result your solution will always be the order of the xml document. If KidList is first in the Xml then it will be first in your output. If you need it to be more generic then you will need to do this: <xsl:for-each select="*"> <xsl:sort select="@order" data-type="number" /> <xsl:variable name="TemplateName" select="."/> <xsl:apply-templates select="$TemplateName"/> </xsl:for-each> That is assuming you are still using the "order" attribute in your xml that was in your first post. Otherwise you'll have to sort by the name of the element in descending order (PeopleList then KidList). <xsl:for-each select="*"> <xsl:sort select="." data-type="text" order="descending" /> <xsl:variable name="TemplateName" select="."/> <xsl:apply-templates select="$TemplateName"/> </xsl:for-each>

            Mark's blog: developMENTALmadness.blogspot.com Funniest variable name: lLongDong - spotted in legacy code, was used to determine how long a beep should be. - Dave Bacher

            Y Offline
            Y Offline
            Yariv
            wrote on last edited by
            #5

            Mark J. Miller wrote:

            If you can't control the order of KidList and PeopleList then the result your solution will always be the order of the xml document.

            Maybe I didn't explain clearly, but that is what I wanted: maintain the xml order. I thought to add the "order" attribute for sorting, but had no idea how to use it. Your code is a great help and I will use it for sure. Thanks :)

            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