Thread: XSL - grouping

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    XSL - grouping


    Hi All

    I am referring an xml file(document.xml), through document function. Based on the input xml id value, I am taking the corresponding value from document.xml. The output will have this corresponding value and the position in input, as shown in the actualoutput.xml.
    However, would like to generate an output xml like expectedOutput.xml, having the attributes Value. Could anyone please point how to change my xsl file to get the expectedOutput.xml?

    below are the files
    XslFile.xsl
    Code:
    <xsl:stylesheet version="1.0" exclude-result-prefixes="msxsl" xmlns:xsl="(URL address blocked: See forum rules)" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
       <xsl:output method="xml" indent="yes"/>
       <xsl:template match="/">
          <result>
             <xsl:apply-templates select="/*/id"/>
          </result>
       </xsl:template>
       <xsl:template match="id">
          <xsl:variable name="currencydetails" select="document('document.xml')/doc"/>
          <xsl:variable name="id">
             <xsl:value-of select="."/>
          </xsl:variable>
          <mapValue>
             <xsl:variable name="fromDocument" select="$currencydetails/id[@value=$id]"/>
             <xsl:value-of select="$fromDocument"/>
          </mapValue>
          <position>
             <xsl:value-of select="position()"/>
          </position>
       </xsl:template>
    </xsl:stylesheet>
    document.xml
    Code:
    <doc>
       <id value="123">abc</id>
       <id value="456">abc</id>
       <id value="011">def</id>
       <id value="rty">ghj</id>
       <id value="iop">qwd</id>
       <id value="321">ply</id>
    </doc>

    input.xml
    Code:
    <Root>
       <id>123</id>
       <id>321</id>
       <id>897</id>
       <id>011</id>
       <id>456</id>
    </Root>

    actualoutput.xml
    Code:
    <result>
       <mapValue>abc</mapValue>
       <position>1</position>
       <mapValue>ply</mapValue>
       <position>2</position>
       <mapValue/>
       <position>3</position>
       <mapValue>def</mapValue>
       <position>4</position>
       <mapValue>abc</mapValue>
       <position>5</position>
    </result>
    expectedOutput.xml
    Code:
    <result>
       <map Value="abc">
          <position>1</position>
          <position>5</position>
       </map>
       <map Value="nomatch">
          <position>3</position>
       </map>
       <map Value="def">
          <position>4</position>
       </map>
       <map Value="ply">
          <position>2</position>
       </map>
    </result>
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    First comes editing your post. All your XML/XSLT is on one line and that makes it nearly impossible to read. Hit the Edit button on your post, remove the stuff you've posted from inside the &amp;#91;code][/code] tags, and copy/paste the originals back in.

    In the future, don't click the Code button and paste your code into the dialog. Instead paste it right into the post, select it all, and then hit the Code button. Or you can type out the &amp;#91;code] and [/code] tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0
    Thanks. I have edited the post to make it readable
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0
    Originally Posted by jaango123
    Thanks. I have edited the post to make it readable

    I changed the xslfile like below, however no luck

    Code:
    <xsl:stylesheet version="1.0" exclude-result-prefixes="msxsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" >
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/">
            <result>
                <xsl:apply-templates select="/*/id"/>
            </result>
        </xsl:template>
        <xsl:template match="id">
            <xsl:variable name="currencydetails" select="document('document.xml')/doc"/>
            <xsl:variable name="id" select="."/>
            <xsl:variable name="fromDocument" select="$currencydetails/id[@value=$id]"/>
            <map value="{$fromDocument}"/>
            <position>
                <xsl:for-each select="$fromDocument">
                    <xsl:number/>
                </xsl:for-each>
            </position>
        </xsl:template>
    </xsl:stylesheet>

IMN logo majestic logo threadwatch logo seochat tools logo