我正在尝试通过以下方式使用match属性来获取XML文件中的一组节点-
<xsl:template match="//title" name="split">
而且似乎不起作用。
这是我要使用的XML文件(取自https://www.w3schools.com/xml/xpath_syntax.asp)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="c.xsl"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="en">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
这是我正在尝试运行的xsl文件
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="//title" name="split">
<xsl:value-of select=".">
</xsl:value-of>
</xsl:template>
</xsl:stylesheet>
所需的输出是:Harry Potter Learning XML
最佳答案
您有一个与标题正确匹配的模板,但是没有任何东西可以捕捉并忽略其他所有内容,因此将包含所有文本内容。
放入一个完全匹配的模板,该模板将样式表重新应用到其他所有模板上。请注意,如果以这种方式进行操作,则不需要通配符//title
匹配,而只需匹配元素名称即可。
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="title" name="split">
<xsl:value-of select="concat(., ' ')" />
</xsl:template>
<xsl:template match="@*|node()">
<xsl:apply-templates select="@*|node()" />
</xsl:template>
</xsl:stylesheet>
输出量
Harry Potter Learning XML