过去几周我一直在研究这个问题,这是我第一次使用 XSLT,所以我很惊讶我能做到这一点。我的问题是我的 XSL 正在工作,但它没有将结果分组到他们自己的节点中。我的 XML:
<r25:space_reservations pubdate="2013-10-11T19:52:51-05:00" xmlns:r25="http://www.collegenet.com/r25" xmlns:xl="http://www.w3.org/1999/xlink">
<r25:space_reservation xl:href="reservation.xml?rsrv_id=4630649">
<r25:reservation_start_dt>2013-10-11T08:25:00-05:00</r25:reservation_start_dt>
<r25:reservation_end_dt>2013-10-11T09:15:00-05:00</r25:reservation_end_dt>
<r25:act_head_count>25</r25:act_head_count>
<r25:shared>F</r25:shared>
<r25:reservation_state>1</r25:reservation_state>
<r25:spaces xl:href="space.xml?space_id=582">
<r25:space_id>582</r25:space_id>
<r25:space_name>HARP 2060</r25:space_name>
<r25:formal_name>Room 2060</r25:formal_name>
<r25:related_space_id />
<r25:related_space_name />
</r25:spaces>
<r25:layout_id>10</r25:layout_id>
<r25:layout_name>SC - AUDIENCE</r25:layout_name>
<r25:reservation_comment_id />
<r25:reservation_comments />
<r25:space_instruction_id>409729</r25:space_instruction_id>
<r25:space_instructions></r25:space_instructions>
<r25:registration_url />
<r25:event xl:href="event.xml?event_id=88949">
<r25:event_id>88949</r25:event_id>
<r25:event_locator>2013-AAFBPD</r25:event_locator>
<r25:event_name>Event Name here</r25:event_name>
<r25:event_title>Event Title here</r25:event_title>
<r25:event_type_name>COURSE-70</r25:event_type_name>
<r25:event_type_class />
<r25:organization_id xl:href="organization.xml?organization_id=37">37</r25:organization_id>
<r25:organization_name>BUS</r25:organization_name>
<r25:pre_event_dt>2013-10-11T08:25:00-05:00</r25:pre_event_dt>
<r25:event_start_dt>2013-10-11T08:25:00-05:00</r25:event_start_dt>
<r25:event_end_dt>2013-10-11T09:15:00-05:00</r25:event_end_dt>
<r25:post_event_dt>2013-10-11T09:15:00-05:00</r25:post_event_dt>
<r25:profile_id>269970</r25:profile_id>
<r25:profile_name>70049-1</r25:profile_name>
<r25:expected_count>25</r25:expected_count>
<r25:registered_count>0</r25:registered_count>
</r25:event>
<r25:last_mod_dt>2013-08-09T14:28:20-05:00</r25:last_mod_dt>
</r25:space_reservation>
<r25:space_reservation xl:href="reservation.xml?rsrv_id=4630700">
<r25:reservation_start_dt>2013-10-11T09:30:00-05:00</r25:reservation_start_dt>
<r25:reservation_end_dt>2013-10-11T10:20:00-05:00</r25:reservation_end_dt>
<r25:act_head_count>25</r25:act_head_count>
<r25:shared>F</r25:shared>
<r25:reservation_state>1</r25:reservation_state>
<r25:spaces xl:href="space.xml?space_id=582">
<r25:space_id>582</r25:space_id>
<r25:space_name>HARP 2060</r25:space_name>
<r25:formal_name>Room 2060</r25:formal_name>
<r25:related_space_id />
<r25:related_space_name />
</r25:spaces>
<r25:layout_id>10</r25:layout_id>
<r25:layout_name>SC - AUDIENCE</r25:layout_name>
<r25:reservation_comment_id />
<r25:reservation_comments />
<r25:space_instruction_id>410075</r25:space_instruction_id>
<r25:space_instructions></r25:space_instructions>
<r25:registration_url />
<r25:event xl:href="event.xml?event_id=88950">
<r25:event_id>88950</r25:event_id>
<r25:event_locator>2013-AAFBPE</r25:event_locator>
<r25:event_name>event name here</r25:event_name>
<r25:event_title>event title here</r25:event_title>
<r25:event_type_name>COURSE-70</r25:event_type_name>
<r25:event_type_class />
<r25:organization_id xl:href="organization.xml?organization_id=37">37</r25:organization_id>
<r25:organization_name>BUS</r25:organization_name>
<r25:pre_event_dt>2013-10-11T09:30:00-05:00</r25:pre_event_dt>
<r25:event_start_dt>2013-10-11T09:30:00-05:00</r25:event_start_dt>
<r25:event_end_dt>2013-10-11T10:20:00-05:00</r25:event_end_dt>
<r25:post_event_dt>2013-10-11T10:20:00-05:00</r25:post_event_dt>
<r25:profile_id>269971</r25:profile_id>
<r25:profile_name>70050-1</r25:profile_name>
<r25:expected_count>25</r25:expected_count>
<r25:registered_count>0</r25:registered_count>
</r25:event>
<r25:last_mod_dt>2013-08-09T11:17:42-05:00</r25:last_mod_dt>
</r25:space_reservation>
</r25:space_reservations>
我的 XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r25="http://www.collegenet.com/r25">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:element name="r25data">
<xsl:apply-templates select="r25:space_reservations/r25:space_reservation/r25:spaces/r25:space_name"/>
<xsl:apply-templates select="r25:space_reservations/r25:space_reservation/r25:event/r25:event_name"/>
<xsl:apply-templates select="r25:space_reservations/r25:space_reservation/r25:event/r25:event_title"/>
<xsl:apply-templates select="r25:space_reservations/r25:space_reservation/r25:event/r25:event_start_dt"/>
<xsl:apply-templates select="r25:space_reservations/r25:space_reservation/r25:event/r25:event_end_dt"/>
</xsl:element>
</xsl:template>
<xsl:template match="r25:space_reservations/r25:space_reservation/r25:spaces/r25:space_name">
<xsl:element name="room" >
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="r25:space_reservations/r25:space_reservation/r25:event/r25:event_name">
<xsl:element name="eventName" >
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="r25:space_reservations/r25:space_reservation/r25:event/r25:event_title">
<xsl:element name="eventTitle" >
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="r25:space_reservations/r25:space_reservation/r25:event/r25:event_start_dt">
<xsl:element name="eventStart" >
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="r25:space_reservations/r25:space_reservation/r25:event/r25:event_end_dt">
<xsl:element name="eventStart" >
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我得到的结果是
<r25data>
<room></room>
<room></room>
<room></room>...
<eventName></eventName>
<eventName></eventName>
<eventName></eventName>
<eventName></eventName>....
等等,等等,等等,通过我想要的不同节点
</r25data>
如何让这个 XSL 样式表在节点中对每个样式表进行分组?本质上,我想要做的是使用 XSL 从 XML 提要中提取我需要的数据,并给我一个简单的方法来解析它。我所需要的只是我请求到它自己更简单的 XML 提要中的元素,然后我可以解析(也没有 r25: 命名空间!)任何帮助将不胜感激!谢谢!