嘿伙计们,我正在使用xml和xslt开展我的第一个项目。现在我想要完成的目标是素食菜单的属性,并在餐馆名称下显示在页面底部。我需要在一行上显示MenuItem,Price和Calories的名称。任何帮助将不胜感激。
好吧,我已经把它全部放在页面底部我应该
小小屋旅馆
菜单项目价格卡路里水果沙拉5.50< 300
Ma和Pa's
菜单项目价格卡路里厨师沙拉5.00 200
这是XML
<?xml version="1.0" encoding="utf-8"?>
<!--Kenny Freeman-->
<Restaurants>
<Restaurant Name="Little Cabin Inn" QualityRating="3" PriceRating="2">
<Location>
<Address>10 Main Street</Address>
<City>Chicago</City>
<State>Illinois</State>
</Location>
<PhoneNumber>555-555-5555</PhoneNumber>
<Menu>
<MenuItem Price="4.95" Calories="450" IsVegetarian="false">Hamburger</MenuItem>
<MenuItem Price="5.50" Calories="<300" IsVegetarian="true">Fruit Salad</MenuItem>
</Menu>
</Restaurant>
<Restaurant Name="Ma and Pa" QualityRating="4" PriceRating="3">
<Location>
<Address>1234 2nd Street</Address>
<City>Chicago</City>
<State>Illinois</State>
</Location>
<PhoneNumber>444-444-4444</PhoneNumber>
<Menu>
<MenuItem Price="4.00" Calories="475" IsVegetarian="false">CheeseBurger</MenuItem>
<MenuItem Price="5.00" Calories="200" IsVegetarian="true">Chef Salad</MenuItem>
</Menu>
</Restaurant>
<Restaurant Name="Meaty Petes Cafe" QualityRating="4" PriceRating="1">
<Location>
<Address>1232 Cedar Drive</Address>
<City>Los Angelas</City>
<State>California</State>
</Location>
<PhoneNumber>333-333-3333</PhoneNumber>
<Menu>
<MenuItem Price="6.00" Calories="500" IsVegetarian="false">Stack of Ribs</MenuItem>
</Menu>
</Restaurant>
</Restaurants>
和XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<head>
<title>Restaurants</title>
</head>
<body>
<h1>Recommended Restaurant</h1>
<p>
In Chicago, I recommend <xsl:value-of select="Restaurants/Restaurant/@Name" />
</p>
<h1>Restaurants by location</h1>
<table border="1">
<tr>
<th>Restaurant Name</th>
<th>City</th>
<th>State</th>
<th>Quality</th>
<th>Price</th>
</tr>
<xsl:for-each select="Restaurants/Restaurant">
<xsl:sort select="Location/State"/>
<tr>
<td>
<xsl:value-of select="@Name"/>
</td>
<td>
<xsl:value-of select="Location/City"/>
</td>
<td>
<xsl:value-of select="Location/State"/>
</td>
<td>
<xsl:for-each select="Restaurants/Restaurant"/>
<xsl:value-of select="@QualityRating"/>
</td>
<td>
<xsl:for-each select="Restaurants/Restaurant"/>
<xsl:value-of select="@PriceRating"/>
</td>
</tr>
</xsl:for-each>
</table>
<!--Restaurants that serve vegetarian-->
<h1>Restaurants with Vegetarian Choices</h1>
<xsl:for-each select="Restaurants/Restaurant">
<xsl:if test="Menu/MenuItem/@IsVegetarian='true'">
<h3>
<xsl:value-of select="@Name"/>
<xsl:for-each select="Menu/MenuItem">
<br>
<xsl:value-of select="Menu/MenuItem"/>
<xsl:value-of select="@Price"/>
<xsl:value-of select="@Calories"/>
</br>
</xsl:for-each>
</h3>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
看起来你做错了两件事。
首先,以下代码段不会包含菜单项元素的文本:
<xsl:value-of select="Menu/MenuItem"/>
您已经选择了元素,因此您可以使用以下内容:
<xsl:value-of select="text()"/>
获取内部文本 - 请参阅xslt: value of text associated with element了解更多
其次,虽然您选择的是带素食菜单的餐馆,但听起来您只想展示素食菜单。因此,您需要将循环更改为仅显示这些项目 - 额外的xsl:if语句可以执行此任务,尽管可以更干净地执行此操作。
答案 1 :(得分:1)
这个XSLT 1.0样式表......
<Restaurants>
<Restaurant Name="Little Cabin Inn" QualityRating="3" PriceRating="2">
<Location>
<Address>10 Main Street</Address>
<City>Chicago</City>
<State>Illinois</State>
</Location>
<PhoneNumber>555-555-5555</PhoneNumber>
<Menu>
<MenuItem Price="4.95" Calories="450" IsVegetarian="false">Hamburger</MenuItem>
<MenuItem Price="5.50" Calories="<300" IsVegetarian="true">Fruit Salad</MenuItem>
</Menu>
</Restaurant>
<Restaurant Name="Ma and Pa" QualityRating="4" PriceRating="3">
<Location>
<Address>1234 2nd Street</Address>
<City>Chicago</City>
<State>Illinois</State>
</Location>
<PhoneNumber>444-444-4444</PhoneNumber>
<Menu>
<MenuItem Price="4.00" Calories="475" IsVegetarian="false">CheeseBurger</MenuItem>
<MenuItem Price="5.00" Calories="200" IsVegetarian="true">Chef Salad</MenuItem>
</Menu>
</Restaurant>
<Restaurant Name="Meaty Petes Cafe" QualityRating="4" PriceRating="1">
<Location>
<Address>1232 Cedar Drive</Address>
<City>Los Angelas</City>
<State>California</State>
</Location>
<PhoneNumber>333-333-3333</PhoneNumber>
<Menu>
<MenuItem Price="6.00" Calories="500" IsVegetarian="false">Stack of Ribs</MenuItem>
</Menu>
</Restaurant>
</Restaurants>
...将转换此输入文档......
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Restaurants</title>
</head>
<body>
<h1>Recommended Restaurant</h1>
<p>
In Chicago, I recommend Little Cabin Inn</p>
<h1>Restaurants by location</h1>
<table border="1">
<tr>
<th>Restaurant Name</th>
<th>City</th>
<th>State</th>
<th>Quality</th>
<th>Price</th>
</tr>
<tr>
<td>Meaty Petes Cafe</td>
<td>Los Angelas</td>
<td>California</td>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>Little Cabin Inn</td>
<td>Chicago</td>
<td>Illinois</td>
<td>3</td>
<td>2</td>
</tr>
<tr>
<td>Ma and Pa</td>
<td>Chicago</td>
<td>Illinois</td>
<td>4</td>
<td>3</td>
</tr>
</table>
<h1>Restaurants with Vegetarian Choices</h1>
<table border="1">
<tr>
<th>Restaurant Name</th>
<th>Menu item</th>
<th>Price</th>
<th>Calories</th>
</tr>
<tr>
<td>Ma and Pa</td>
<td>Chef Salad</td>
<td>5.00</td>
<td>200</td>
</tr>
<tr>
<td>Little Cabin Inn</td>
<td>Fruit Salad</td>
<td>5.50</td>
<td><300</td>
</tr>
</table>
</body>
</html>
...进入此输出文档...
SELECT wpp.ID,wpp.post_title,wpp.guid,
(SELECT guid FROM wp_posts WHERE post_parent = (SELECT ID FROM wp_posts WHERE post_title =CONCAT("Writer Thumbnail Image ",wpp.ID) )) AS imgID
FROM `wp_posts` AS wpp WHERE wpp.post_status = "publish" AND wpp.post_type = "post" ORDER BY wpp.ID DESC LIMIT 4