我想使用xsltproc转换xml文件并仅提取它的一些部分,我有xslt像这样:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="xml"
indent="yes"
encoding="iso-8859-1" />
<xsl:template match="glossary">
<ul>
<xsl:for-each select="*/glossentry">
<li>
<h2><xsl:value-of select="glossterm"/> (<xsl:value-of select="abbrev/emphasis"/>)</h2>
<div><xsl:value-of select="*/para"/></div>
</li>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
但它显示xml内部的所有其他文本作为文本。 需要添加或更改的内容只显示类似内容?
<html><body>
<ul>
<li>
<h2>Term (abbrev)</h2>
<div>Para</div>
</li>
<li>
<h2>Term2 (abbrev2)</h2>
<div>Para2</div>
</li>
...
</ul>
答案 0 :(得分:1)
好的,我找到了,我需要将select添加到apply-templates
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="xml"
indent="yes"
encoding="iso-8859-1" />
<xsl:template match="part">
<xsl:if test="@id = 'lexicon'">
<xsl:apply-templates select="glossary"/>
</xsl:if>
</xsl:template>
<xsl:template match="glossary">
<ul>
<xsl:for-each select="*/glossentry">
<li>
<h2><xsl:value-of select="glossterm"/>
<xsl:if test="abbrev">
<xsl:text>: </xsl:text>
<xsl:for-each select="abbrev/*">
<xsl:if test="position() > 1">, </xsl:if>
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:if>
</h2>
<div><xsl:value-of select="*/para"/></div>
</li>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template match="book">
<html>
<title><xsl:value-of select="title"/></title>
<body>
<xsl:apply-templates select="part"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>