显示Java中两个不同XML文件的结果

时间:2015-10-17 06:33:39

标签: java xml xslt

因此,我尝试使用XSLT从两个不同的XML文件中提取结果,以显示“餐厅评论”。我在allRestaurants.xml中有餐厅的详细信息,并在allReviews.xml中拥有这些餐厅的所有评论。我目前在每家餐厅都存放了一个标签,评论也与特定餐厅相关联,因此请携带相同的标签。我需要建立一个页面,其中ID为1的餐厅和下方的餐厅显示该餐厅的评论。评论与下面完全相同的1存储。请帮忙。

allRestaurants.xml

<restaurants>
<restaurant>
    <restaurant_id>1</restaurant_id>
    <name>The Jackaroo</name>
    <street_address>107-109 Darlinghurst Road</street_address>
    <postcode>2011</postcode>
    <city>Sydney</city>
    <state>NSW</state>
    <country>Australia</country>
    <email>info@jackaroo.com.au</email>
    <telephone>93322244</telephone>
    <stars>3</stars>
</restaurant>
<restaurant>
    <restaurant_id>2</restaurant_id>
    <name>Four Seasons restaurant Sydney</name>
    <street_address>199 George Street</street_address>
    <postcode>2000</postcode>
    <city>Sydney</city>
    <state>NSW</state>
    <country>Australia</country>
    <email>info@sydneyfourseasons.com.au</email>
    <telephone>92503100</telephone>
    <stars>5</stars>
</restaurant>
</restaurants>

allReviews.xml

<reviews>
<review id="1">
    <restaurant_id>1</restaurant_id>
    <author_id>1</author_id>
    <headline>Clean Bare-Bones Hostel</headline>
    <details>
        Example text here
    </details>
    <rating>3</rating>
    <date>1388782853</date>
</review>
<review id="2">
    <restaurant_id>1</restaurant_id>
    <author_id>3</author_id>
    <headline>Wouldn't Recommend</headline>
    <details>
        Example text here
    </details>
    <rating>2</rating>
    <date>1368748800</date>
</review>
<review id="3">
    <restaurant_id>2</restaurant_id>
    <author_id>2</author_id>
    <headline>Overall I Enjoyed</headline>
    <details>
        Example text here
    </details>
    <rating>4</rating>
    <date>1378788850</date>
</review>
</reviews>

我认为可能会将它们合并到一个XML文件中,这样就可以了,但即便如此,我也不确定从哪里开始:

oneHotel.xml

<?xml-stylesheet type="text/xsl" href="oneHotel.xsl"?>
<list>
    <entry name="allHotels.xml" />
    <entry name="reviews.xml" />
</list>

就我在XSLT文档中所做的而言,这是一个巨大的空白。我甚至不知道从哪里开始:

oneHotel.xsl

<xsl:stylesheet version="1.0"    
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:copy-of name="restaurant" select="document('allRestaurants.xml') 
       /restaurants/restaurant[restaurant_id=1]"/>
<xsl:copy-of name="reviews" select="document('allReviews.xml') 
       /reviews/review[restaurant_id=1]"/>

<xsl:template match="/">

<xsl:choose>
    <xsl:when test="document('allRestaurants.xml') 
           /restaurants/restaurant[restaurant_id=1]"/>
        <h2><xsl:value-of select="name"/></h2>
</xsl:choose>
<h2><xsl:value-of select="$restaurant/name"/></h2>


</xsl:template>

</xsl:stylesheet> 

1 个答案:

答案 0 :(得分:1)

以此为出发点:

XSLT 1.0

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

<xsl:param name="path-to-reviews" select="'allReviews.xml'"/>

<xsl:key name="review-by-restaurant-id" match="review" use="restaurant_id" />

<xsl:template match="/restaurants">
    <html>
        <body>
            <h1>Restaurant Reviews</h1>
            <xsl:apply-templates select="restaurant"/>
        </body>
    </html>
</xsl:template>

<xsl:template match="restaurant">
    <h2>
        <xsl:value-of select="name"/>
    </h2>
    <xsl:variable name="id" select="restaurant_id" />
    <!-- switch context to lookup document in order to use key -->
    <xsl:for-each select="document($path-to-reviews)">
        <xsl:for-each select="key('review-by-restaurant-id', $id)">
            <h3>
                <xsl:value-of select="headline"/>
            </h3>
            <p>
                <xsl:value-of select="details"/>
            </p>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

这假设您指示XSLT处理器处理allRestaurants.xml文档并将路径作为参数传递给allReviews.xml文档。

您没有告诉我们您希望最终结果如何,所以我只是编写了一个非常基本的页面。