我有一个用于房地产属性列表的XML文件 - 其中一个名为'AdvNotes'的字段包含该属性的功能。这些通常以纯文本形式输入如下:
Property features:
- feature one
- feature two
- feature three
- feature four
- etc
我想将这些行转换为HTML列表,因此我的输出变为......
Property features:
<ul><li>
feature one
</li><li> feature two
</li><li> feature three
</li><li> feature four
</li><li> etc
</li></ul>
任何人都可以提供一个解决方案,了解如何使用XSLT ver 1实现这一目标吗?
完整的XSL文件(没有AdvNotes模板)如下:
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="NewInTown_20110918170305.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
<xsl:param name="PropertyID"/>
<xsl:key name="propertylist" match="Properties/Property" use="@PropertyID" />
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>REST Property Detail</title>
</head>
<body class="container_12 clearfix">
<xsl:for-each select="key('propertylist',$PropertyID)">
<h2><xsl:value-of select="StreetNumber"/> <xsl:value-of select="StreetName"/>, <xsl:value-of select="Suburb"/></h2>
<div class="grid_9">
<div id="propertySlides">
<xsl:for-each select="Images/Image">
<img src="/storage/rest/{@FileName}" title="Image {@ImageID}" alt="Image {@ImageID}" />
</xsl:for-each>
</div>
<h3 class="grid_8"><xsl:value-of select="AdvHeading"/></h3>
<div class="grid_4 alpha" id="AdvDescription"><xsl:value-of select="AdvDescription"/></div>
<div class="grid_4 omega" id="AdvNotes"><xsl:value-of select="AdvNotes"/></div>
</div>
<div class="grid_3">
<h4 id="propStats"><xsl:value-of select="Features/Bedrooms"/> <img src="/storage/gfx/bed_icon.gif" alt="Bedrooms" width="24" height="16" /> <xsl:value-of select="Features/Bathrooms"/> <img src="/storage/gfx/bath_icon.gif" alt="Bathrooms" width="24" height="16" /> <xsl:value-of select="Features/Garages"/> <img src="/storage/gfx/car_icon.gif" alt="Garages" width="24" height="16" /></h4>
<div id="gmap">
<h6>Map Reference: <xsl:value-of select="MapReference"/> (p<xsl:value-of select="MapPage"/>)</h6>
</div>
<h6>Available: <xsl:value-of select="DateAvailable"/></h6>
<h3>$<xsl:value-of select="Rent"/> per week</h3>
<ul id="propertyFeatures">
<li id="LandArea"><strong>Land Area</strong>m<sup>2</sup><xsl:value-of select="LandArea"/></li>
<li><strong>Building Area</strong> <xsl:value-of select="BuildingArea"/>m<sup>2</sup></li>
<li><strong>Heating</strong> <xsl:value-of select="Features/Heating/@Type"/></li>
<li><strong>Hot Water</strong> <xsl:value-of select="Features/HotWater/@Type"/></li>
<li><strong>Laundry</strong> <xsl:value-of select="Features/Laundry/@Type"/></li>
<xsl:if test="contains(Features/AirConditioning,'1')">
<li><strong>Air Con</strong> <img src="/storage/gfx/tick.png" alt="Air Conditioning" /></li>
</xsl:if>
<xsl:if test="contains(Features/Ensuite,'1')">
<li><strong>Ensuite</strong> <img src="/storage/gfx/tick.png" alt="Ensuite" /></li>
</xsl:if>
<xsl:if test="contains(Features/Alarm,'1')">
<li><strong>Alarm</strong> <img src="/storage/gfx/tick.png" alt="Alarm" /></li>
</xsl:if>
<xsl:if test="contains(Features/Pool,'1')">
<li><strong>Pool</strong> <img src="/storage/gfx/tick.png" alt="Pool" /></li>
</xsl:if>
<xsl:if test="contains(Features/Spa,'1')">
<li><strong>Spa</strong> <img src="/storage/gfx/tick.png" alt="" /></li>
</xsl:if>
<xsl:if test="contains(Features/Balcony,'1')">
<li><strong>Balcony</strong> <img src="/storage/gfx/tick.png" alt="Balcony" /></li>
</xsl:if>
<xsl:if test="contains(Features/BuildingSecurity,'1')">
<li><strong>Security</strong> <img src="/storage/gfx/tick.png" alt="Building Security" /></li>
</xsl:if>
<xsl:if test="contains(Features/NearShops,'1')">
<li><strong>Near Shops</strong> <img src="/storage/gfx/tick.png" alt="Near Shops" /></li>
</xsl:if>
<xsl:if test="contains(Features/NearTransport,'1')">
<li><strong>Near Transport</strong> <img src="/storage/gfx/tick.png" alt="Near Transport" /></li>
</xsl:if>
<xsl:if test="contains(Features/NearBeach,'1')">
<li><strong>Near Beach</strong> <img src="/storage/gfx/tick.png" alt="Near Beach" /></li>
</xsl:if>
<xsl:if test="contains(Features/CityViews,'1')">
<li><strong>City Views</strong> <img src="/storage/gfx/tick.png" alt="City Views" /></li>
</xsl:if>
<xsl:if test="contains(Features/WaterViews,'1')">
<li><strong>River Views</strong> <img src="/storage/gfx/tick.png" alt="River Views" /></li>
</xsl:if>
<xsl:if test="contains(Features/Intercom,'1')">
<li id="Intercom"><strong>Intercom</strong> <img src="/storage/gfx/tick.png" alt="xxxx" /></li>
</xsl:if>
<xsl:if test="contains(Features/Vacuum,'1')">
<li id="Vacuum"><strong>Vacuum</strong> <img src="/storage/gfx/tick.png" alt="Vacuum" /></li>
</xsl:if>
<xsl:if test="contains(Features/FirePlace,'1')">
<li id="Fireplace"><strong>Fireplace</strong> <img src="/storage/gfx/tick.png" alt="FirePlace" /></li>
</xsl:if>
<xsl:if test="contains(Features/Furnished,'1')">
<li id="Furnished"><strong>Furnished</strong> <img src="/storage/gfx/tick.png" alt="Furnished" /></li>
</xsl:if>
<li style="clear:both;"></li>
</ul>
<h3><strong>Interested?</strong></h3>
<p>Contact the property agent directly or <a href="/contact/">make an enquiry here</a>.</p>
<h4><a><xsl:attribute name="href">mailto:<xsl:value-of select="/Contacts/Contact/Email" />?subject=<xsl:value-of select="StreetNumber" /> <xsl:value-of select="StreetName" /> <xsl:value-of select="Suburb" /></xsl:attribute>Agent Name</a><br />
mobile</h4>
<p>Phone: BHphone<br />
After hours: AHphone</p>
</div>
</xsl:for-each>
<script language="javascript">
$(document).ready(function(){
$('#propertySlides').innerfade({
speed: 'slow',
timeout: 5000,
type: 'sequence',
containerheight: '420px'
});
});
</script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
这是为了让你入门。如果连字符也可能出现在列表项中,事情会变得复杂得多。
XSLT 2.0(由标记化分割)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="AdvNotes">
<xsl:variable name="tokens" select="tokenize(.,'-')"/>
<p>
<xsl:value-of select="$tokens[1]"/>
</p>
<ul>
<xsl:for-each select="remove($tokens,1)">
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
XSLT 1.0(通过递归分割)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="AdvNotes">
<xsl:variable name="p" select="substring-before(.,'-')"/>
<p>
<xsl:value-of select="$p"/>
</p>
<ul>
<xsl:call-template name="to_list"/>
</ul>
</xsl:template>
<xsl:template name="to_list">
<xsl:param name="li" select="substring-after(.,'-')"/>
<xsl:variable name="item" select="substring-before($li,'-')"/>
<xsl:choose>
<xsl:when test="not($item)">
<li>
<xsl:value-of select="$li"/>
</li>
</xsl:when>
<xsl:otherwise>
<li>
<xsl:value-of select="$item"/>
</li>
<xsl:call-template name="to_list">
<xsl:with-param name="li" select="substring-after($li,'-')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
两种转换产生:
<p>
Property features:
</p>
<ul>
<li> feature one
</li>
<li> feature two
</li>
<li> feature three
</li>
<li> feature four
</li>
<li> etc
</li>
</ul>