過去幾周我一直在爲此工作,這是我第一次使用XSLT,所以我很驚訝我已經做到了這一點。我的問題是我的XSL正在工作,但它不會將結果分組到他們自己的節點中。 我的XML:用XSLT將XML轉換爲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 Feed中提取我需要的數據,並給我一個簡單的方法來解析它。我所需要的是我已經請求的元素,它是我自己可以解析的更簡單的XML feed(不包括r25:命名空間!) 任何幫助將不勝感激!謝謝!
假設你現在已經完成了這項工作,但看起來你需要一個r25的模板:space_reservation,因爲這是你想要的信息的所有項目的父項。如果您需要更多,請留下評論。 – Christina