Я пытаюсь связать значения RestaurantId
в узле ReviewApplication/Reviews/Review
с Restaurant/Name
в Companys/Company/Restaurants/Restaurant
узле в файле XSL, но я не знаю, как это сделать, я пробовал <choose>
и <key>
, но в настоящее время он у меня не работает .. Я очень ценю любой ввод!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:key name="firstname" match="ReviewApplication/Reviews/Review/UserName" use="ReviewApplication/Users/User/FirstName" />
<xsl:output method="html" indent="yes"></xsl:output>
<xsl:template match="/ReviewApplication">
<html><head><title>Reviews with Recommendations</title></head><body>
<table border="1">
<tr>
<th>RestaurantName</th>
<th>FirstName</th>
<th>Rating</th>
</tr>
<xsl:for-each select="/ReviewApplication/Reviews/Review">
<xsl:variable name="restaurantname" select="/ReviewApplication/Companys/Company/Restaurants/Restaurant/Name" />
<xsl:variable name="name" select="../../Users/User/FirstName" />
<tr>
<td>
<xsl:value-of select="RestaurantId"></xsl:value-of>
</td>
<td>
<xsl:value-of select="UserName"></xsl:value-of>
</td>
<td>
<xsl:value-of select="Rating"></xsl:value-of>
</td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
Мой XML файл:
<?xml version="1.0" encoding="UTF-8"?>
<ReviewApplication>
<Users>
<User>
<UserName>username1</UserName>
<FirstName>first1</FirstName>
<LastName>last1</LastName>
</User>
<User>
<UserName>username2</UserName>
<FirstName>first2</FirstName>
<LastName>last2</LastName>
</User>
<User>
<UserName>username3</UserName>
<FirstName>first3</FirstName>
<LastName>last3</LastName>
</User>
<User>
<UserName>username4</UserName>
<FirstName>first4</FirstName>
<LastName>last4</LastName>
</User>
<User>
<UserName>username5</UserName>
<FirstName>first5</FirstName>
<LastName>last5</LastName>
</User>
</Users>
<Companys>
<Company>
<CompanyName>companyname1</CompanyName>
<About>aboutcompany1</About>
<Restaurants>
<Restaurant>
<RestaurantId>restaurantid1</RestaurantId>
<Name>restaurant1</Name>
</Restaurant>
<Restaurant>
<RestaurantId>restaurantid2</RestaurantId>
<Name>restaurant2</Name>
</Restaurant>
<Restaurant>
<RestaurantId>restaurantid3</RestaurantId>
<Name>restaurant3</Name>
</Restaurant>
</Restaurants>
</Company>
<Company>
<CompanyName>companyname2</CompanyName>
<About>aboutcompany2</About>
<Restaurants>
<Restaurant>
<RestaurantId>restaurantid4</RestaurantId>
<Name>restaurant4</Name>
</Restaurant>
<Restaurant>
<RestaurantId>restaurantid5</RestaurantId>
<Name>restaurant5</Name>
</Restaurant>
<Restaurant>
<RestaurantId>restaurantid6</RestaurantId>
<Name>restaurant6</Name>
</Restaurant>
</Restaurants>
</Company>
<Company>
<CompanyName>companyname3</CompanyName>
<About>aboutcompany3</About>
<Restaurants>
<Restaurant>
<RestaurantId>restaurantid7</RestaurantId>
<Name>restaurant7</Name>
</Restaurant>
<Restaurant>
<RestaurantId>restaurantid8</RestaurantId>
<Name>restaurant8</Name>
</Restaurant>
<Restaurant>
<RestaurantId>restaurantid9</RestaurantId>
<Name>restaurant9</Name>
</Restaurant>
</Restaurants>
</Company>
</Companys>
<Reviews>
<Review>
<UserName>username1</UserName>
<RestaurantId>restaurantid1</RestaurantId>
<Rating>4.0</Rating>
</Review>
<Review>
<UserName>username1</UserName>
<RestaurantId>restaurantid2</RestaurantId>
<Rating>5.0</Rating>
</Review>
<Review>
<UserName>username1</UserName>
<RestaurantId>restaurantid4</RestaurantId>
<Rating>4.0</Rating>
</Review>
<Review>
<UserName>username1</UserName>
<RestaurantId>restaurantid5</RestaurantId>
<Rating>1.0</Rating>
</Review>
<Review>
<UserName>username2</UserName>
<RestaurantId>restaurantid1</RestaurantId>
<Rating>2.0</Rating>
</Review>
<Review>
<UserName>username2</UserName>
<RestaurantId>restaurantid7</RestaurantId>
<Rating>3.0</Rating>
</Review>
<Review>
<UserName>username2</UserName>
<RestaurantId>restaurantid8</RestaurantId>
<Rating>4.0</Rating>
</Review>
</Reviews>
<Recommendations>
<Recommendation>
<UserName>username1</UserName>
<RestaurantId>restaurantid1</RestaurantId>
</Recommendation>
<Recommendation>
<UserName>username1</UserName>
<RestaurantId>restaurantid2</RestaurantId>
</Recommendation>
<Recommendation>
<UserName>username1</UserName>
<RestaurantId>restaurantid4</RestaurantId>
</Recommendation>
<Recommendation>
<UserName>username1</UserName>
<RestaurantId>restaurantid5</RestaurantId>
</Recommendation>
<Recommendation>
<UserName>username3</UserName>
<RestaurantId>restaurantid1</RestaurantId>
</Recommendation>
<Recommendation>
<UserName>username4</UserName>
<RestaurantId>restaurantid7</RestaurantId>
</Recommendation>
<Recommendation>
<UserName>username5</UserName>
<RestaurantId>restaurantid8</RestaurantId>
</Recommendation>
</Recommendations>
</ReviewApplication>
EDIT - Ожидаемый результат:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HW 5.10</title>
</head>
<body>
<h1>Reviews with Details</h1>
<table border="1">
<tr>
<th>RestaurantName</th>
<th>FirstName</th>
<th>Rating</th>
</tr>
<tr>
<td>restaurant1</td>
<td>first1</td>
<td>4.0</td>
</tr>
<tr>
<td>restaurant2</td>
<td>first1</td>
<td>5.0</td>
</tr>
<tr>
<td>restaurant4</td>
<td>first1</td>
<td>4.0</td>
</tr>
<tr>
<td>restaurant5</td>
<td>first1</td>
<td>1.0</td>
</tr>
<tr>
<td>restaurant1</td>
<td>first2</td>
<td>2.0</td>
</tr>
<tr>
<td>restaurant7</td>
<td>first2</td>
<td>3.0</td>
</tr>
<tr>
<td>restaurant8</td>
<td>first2</td>
<td>4.0</td>
</tr>
</table>
</body>
</html>