English 中文(简体)
任何人都想写XSLT这一xml的代码?
原标题:anyone please write XSLT code for this xml?
  • 时间:2012-05-05 16:14:14
  •  标签:
  • xslt
<?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>

我想要的是像产出一样的奇数产出。

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

我正试图这样做,但确实没有工作。

<?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>
问题回答

你也许必须改革,因为我删除了你的许多内容,以简化,但是为了解决你的问题,尝试一个“带”;xsl:key>创建教室。

The <xsl:key> Let s we establish a'hmap/dictionary using the data node as Payload and the content of data s first importance as the 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




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...