这是我的xml文件:
<HarperCollins>
<BookComingSoon>
<BookDescription>
After a family tragedy...
<NewParagraph/>
Holden Cameron was addicted...
<NewParagraph/>
When equipment, treasure...
</BookDescription>
<BookComingSoon>
</HarperCollins>
我必须用XML更改它(BookDescription部分):
<blurb>
<p>
Example text example text example text, woo!
</p>
<p>
More example text. Amazing!
</p>
</blurb>
我如何使用XSL(1.0)?
这是我到目前为止所得到的:
<blurb>
<xsl:for-each select="BookDescription">
<p><xsl:value-of select="."/></p>
</xsl:for-each>
</blurb>
修改
这是完整的xml doc:
<?xml version="1.0"?>
<HarperCollins>
<BookComingSoon>
<Book>Night Diver</Book>
<By>Elizabeth Lowell</By>
<Pages>368</Pages>
<CoverImage>nd-el.jpg</CoverImage>
<OnSale>4/8/2014</OnSale>
<ForFansOf>Romance</ForFansOf>
<ForFansOf>Suspense</ForFansOf>
<BookDescription>
After a family tragedy...
<NewParagraph/>
Holden Cameron...
<NewParagraph/>
When equipment, treasure...
</BookDescription>
</BookComingSoon>
<BookComingSoon>
<Book>Under a Silent Moon</Book>
<By>Elizabeth Haynes</By>
<Pages>352</Pages>
<CoverImage>uasm-eh.jpg</CoverImage>
<OnSale>4/15/2014</OnSale>
<ForFansOf>Suspense</ForFansOf>
<ForFansOf>Thriller</ForFansOf>
<BookDescription>
In the crisp, ...
<NewParagraph />
As DCI Louisa Smith ...
</BookDescription>
</BookComingSoon>
</HarperCollins>
我使用的for-each是两个节点。
答案 0 :(得分:2)
可以更简单。您可以通过从不匹配NewParagraph元素来避免空模板。
<xsl:template match="/">
<blurb><xsl:apply-templates match="//BookDescription/text()" /></blurb>
</xsl:template>
<xsl:template match="BookDescription/text()">
<p><xsl:value-of select="."/></p>
</xsl:template>
答案 1 :(得分:0)
For-each完全被滥用:-) 试试这个:
<xsl:template match="BookComingSoon">
<blurb><xsl:apply-templates match="BookDescription" /></blurb>
</xsl:template>
<xsl:template match="BookDescription/text()">
<p><xsl:value-of select="."/></p>
</xsl:template>
<xsl:template match="NewParagraph" />
第一个将开始处理书籍描述。第二个拉出所有文本并用&lt; p&gt;包围它,最后一个用于抑制不需要的元素。