Select once from duplicate XML info, display rest,

2019-08-15 03:56发布

I have an XML doc that looks like this.

<Results>
  <Name>Lab Asst1 </Name>
  <Subject> Math </Subject>
  <Marks>96</Marks>
  <Grade>A</Grade>

  <Name>Student1</Name>
  <Subject>Math</Subject>
  <Marks>90</Marks>
  <Grade>A</Grade>

  <Name>Student1</Name>
  <Subject>English</Subject>
  <Marks>70</Marks>
  <Grade>B</Grade>

  <Name>Lab Asst1 </Name>
  <Subject> Science</Subject>
  <Marks>99</Marks>
  <Grade>A</Grade>

    <Name>Student2</Name>
    <Subject>Science</Subject>
    <Marks>70</Marks>
    <Grade>B</Grade>

  </Results>

Using XSL, what is the simplest way to display, without showing the <Name> element twice? I would like to display the Lab Asst1 results first, assuming there is a text box that uses this value (comes in from C# code): Basically, something that does this part first:

    User: <Name>
    Your results are:
    <table> 
   <tr>
     <td> Subject </td>
     <td> Marks </td> 
     <td> Grade </td>
   </tr>

then call another template etc. or do a for-each or something....

<tr>
     <td> <xsl:value-of select="Subject"/>  </td>
     <td> <xsl:value-of select="Marks"/>  </td> 
     <td>  <xsl:value-of select="Grade"/> </td>
   </tr>

Such that my results appear like this:

User: Lab Asst1
        Your results are:
        Subject | Marks | Grade 
    -------------------------------------
         Science|  99   |   A
           Math |  96   |   A


        User:Student1
        Your results are:
        Subject | Marks | Grade 
    -----------------------------
           Math |  95   |   A
        English |  70   |   B

        User:Student2
        Your results are:
        Subject | Marks | Grade 
    -----------------------------
           Math |  70   |   B

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-15 04:12

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kPersByName" match="Name" use="."/>
 <xsl:key name="kData" match="*"
          use="preceding-sibling::Name[1]"/>

 <xsl:template match="/*">
  <xsl:for-each select=
   "Name[generate-id()
        =
         generate-id(key('kPersByName', .)[1])
        ]
   ">
   <xsl:variable name="vData" select="key('kData', .)"/>

    User: <xsl:value-of select="."/>
    Your results are:
    <table border="1">
       <tr>
         <td> Subject </td>
         <td> Marks </td>
         <td> Grade </td>
       </tr>

       <xsl:for-each select="$vData[self::Subject]">
        <tr>
          <td><xsl:value-of select="."/></td>
          <td><xsl:value-of select="following-sibling::Marks[1]"/></td>
          <td><xsl:value-of select="following-sibling::Grade[1]"/></td>
        </tr>
       </xsl:for-each>
    </table>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when applied to the provided XML document:

<Results>
  <Name>Lab Asst1 </Name>
  <Subject> Math </Subject>
  <Marks>96</Marks>
  <Grade>A</Grade>

  <Name>Student1</Name>
  <Subject>Math</Subject>
  <Marks>90</Marks>
  <Grade>A</Grade>

  <Name>Student1</Name>
  <Subject>English</Subject>
  <Marks>70</Marks>
  <Grade>B</Grade>

  <Name>Lab Asst1 </Name>
  <Subject> Science</Subject>
  <Marks>99</Marks>
  <Grade>A</Grade>

  <Name>Student2</Name>
  <Subject>Science</Subject>
  <Marks>70</Marks>
  <Grade>B</Grade>
</Results>

produces the wanted, correct results:

    User: Lab Asst1 
    Your results are:
    <table border="1">  <tr>
        <td> Subject </td>
        <td> Marks </td>
        <td> Grade </td>
    </tr>
    <tr>
        <td> Math </td>
        <td>96</td>
        <td>A</td>
    </tr>
    <tr>
        <td> Science</td>
        <td>99</td>
        <td>A</td>
    </tr>
</table>

    User: Student1
    Your results are:
    <table border="1">
    <tr>
        <td> Subject </td>
        <td> Marks </td>
        <td> Grade </td>
    </tr>
    <tr>
        <td>Math</td>
        <td>90</td>
        <td>A</td>
    </tr>
    <tr>
        <td>English</td>
        <td>70</td>
        <td>B</td>
    </tr>
</table>

    User: Student2
    Your results are:
    <table border="1">
    <tr>
        <td> Subject </td>
        <td> Marks </td>
        <td> Grade </td>
    </tr>
    <tr>
        <td>Science</td>
        <td>70</td>
        <td>B</td>
    </tr>
</table>

Do note:

  1. The Muenchian method for grouping is used.

  2. In XSLT 2.0 it is easier and more convenient to use the <xsl:for-each-group> instruction.

查看更多
登录 后发表回答