试图在xslt中只检索一个节点值

时间:2014-12-30 12:34:17

标签: xml xslt

您好我有这两个文件:

Ej1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="prue1.xslt"?>
<isa:ISAApplication xmlns:isa="http://xxxx">
    <refId>olatesting</refId>
    <refId2>olatesting2</refId2>
</isa:ISAApplication>

prue1.xslt:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="ISAApplication">
        <html>
            <body>
                <xsl:value-of select="/refId"/>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我只想检索第一个“refId”,但它显示如下:

olatestingolatesting2

我怎样才能得到“olatesting”

1 个答案:

答案 0 :(得分:0)

您必须将isa名称空间添加到样式表声明中,并将前缀添加到模板匹配模式中,例如

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:isa="http://xxxx" version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  exclude-result-prefixes="isa">
 <xsl:output omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
  <xsl:template match="isa:ISAApplication">
    <xsl:value-of select="refId"/>
  </xsl:template>
</xsl:stylesheet>

返回

olatesting

作为辅助信息 - 以前,由于缺少命名空间,您的模板根本不匹配输入XML - 即使您要从XSL中删除模板,您仍然会获得输入XML的两个值 -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"  encoding="utf-8"/> 
</xsl:stylesheet>

应用于输入XML时返回

olatesting
olatesting2

但这可能取决于所使用的XSLT处理器(使用Saxon和Xalan测试)。