-2
<?xml version="1.0" encoding="UTF-8"?>
<school>
<classes>
    <class>
        <name>DEPT-NAME</name>
        <place>ROOM-NO</place>
    </class>
</classes>
<alldata>
    <data>
        <value>CSE</value>
        <value>101</value>
    </data>
    <data>
        <value>IT</value>
        <value>202</value>
    </data>
</alldata>
<students>
    <student>
        <DEPT-NAME>CSE</DEPT-NAME>
        <name>Jhon</name>
        <roll>111</roll>
    </student>
    <student>
        <DEPT-NAME>CSE</DEPT-NAME>
        <name>Zubi</name>
        <roll>112</roll>
    </student>
    <student>
        <DEPT-NAME>IT</DEPT-NAME>
        <name>Jack</name>
        <roll>121</roll>
    </student>
    <student>
        <DEPT-NAME>IT</DEPT-NAME>
        <name>Razz</name>
        <roll>122</roll>
    </student>
</students>
</school>

I want a csv output like

  1. DEPT-NAME ROOM-NO NAME ROLLNO
  2. CSE 101 Jhon 111
  3. __________Zubi 112
  4. IT 202 Jack 121
  5. __________Razz 122

I was trying to do like this way but it did't work

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
        <xsl:template match="/">
        <xsl:text>SCHOOL</xsl:text>

        <xsl:variable name="StudentCSE">
            <xsl:for-each select="school/students/student[DEPT-NAME='CSE']">
                <xsl:value-of select="name"/><xsl:text>,</xsl:text>
                <xsl:value-of select="roll"/>
            </xsl:for-each>
        </xsl:variable>

        <xsl:variable name="Value">
        <xsl:for-each select="school/alldata/data">
                    <xsl:text>&#010;</xsl:text>
                    <xsl:for-each select="value" >
                        <xsl:value-of select="."/><xsl:text>,</xsl:text>
                    </xsl:for-each>
                            <xsl:copy-of select="$StudentCSE"/>
                    </xsl:for-each>
        </xsl:variable>

        <xsl:for-each select="school/classes/class">
                <xsl:text>&#010;</xsl:text>
                <xsl:value-of select="name"/><xsl:text>,</xsl:text>
                <xsl:value-of select="place"/><xsl:text>,NAME,ROll</xsl:text>
                <xsl:copy-of select="$Value"/>
        </xsl:for-each>
        </xsl:template>
</xsl:stylesheet>
4

1 回答 1

2

您可能需要重新格式化,因为为简单起见,我删除了您的许多元素,但要解决您的问题,请尝试使用 <xsl:key> 创建教室查找。

<xsl:key> 让我们使用数据节点作为有效负载并使用数据的第一个值元素的内容作为键来创建一个哈希映射/字典:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>

   <xsl:key name="class-lookup" match="school/alldata/data" use="value[1]" />

   <xsl:template match="/">
      <!-- header -->
      <xsl:for-each select="school/classes/class">
         <xsl:value-of select="name"/>, <xsl:value-of select="place"/>, NAME, ROll
      </xsl:for-each>

      <!-- data -->
      <xsl:for-each select="school/students/*">
         <xsl:text>&#10;</xsl:text>
         <xsl:value-of select="DEPT-NAME"/>, <xsl:value-of select="key('class-lookup', DEPT-NAME)/value[2]" />, <xsl:value-of select="name"/>,  <xsl:value-of select="roll"/>
      </xsl:for-each>

   </xsl:template>
</xsl:stylesheet>

输出:

DEPT-NAME, ROOM-NO, NAME, ROll

CSE, 101, Jhon,  111
CSE, 101, Zubi,  112
IT, 202, Jack,  121
IT, 202, Razz,  122
于 2012-05-05T19:06:00.750 回答