xslt - 从 xml 文件中选择一个属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10183459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xslt - selecting an attribute value from an xml file
提问by rkk
I have a XML file as:
我有一个 XML 文件:
<BatchTable>
<UUThref SocketIndex='0 - CCM'
UUTResult='Passed'
URL='C:\OverrideCallbacks_BatchReport[4 16 2012][4 14 18 PM].xml'
FileName='OverrideCallbacks_BatchReport[4 16 2012][4 14 18 PM].xml'
ECAFailCount='1'
Version='StationPartNumber=55555StationSerialNumber=2222TPSPartNumber=1234'/>
</BatchTable>
In order to pick the Version in the xsl file I have:
为了在 xsl 文件中选择版本,我有:
<xsl:value-of select="BatchTable/UUThref/[@Version]"/>
This does not return any value. What am I doing wrong?
这不会返回任何值。我究竟做错了什么?
回答by Charles Duffy
It should be
它应该是
BatchTable/UUThref/@Version
not
不是
BatchTable/UUThref/[@Version]
...where are you getting the square brackets from?
...你从哪里得到方括号?
I've tested the following, and it definitely works:
我已经测试了以下内容,它绝对有效:
xmlstarlet sel -t -m 'BatchTable/UUThref/@Version' -v . <test.xml
...this command line works by applying the following XSLT:
...此命令行通过应用以下 XSLT 来工作:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:for-each select="BatchTable/UUThref/@Version">
<xsl:call-template name="value-of-template">
<xsl:with-param name="select" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="value-of-template">
<xsl:param name="select"/>
<xsl:value-of select="$select"/>
<xsl:for-each select="exslt:node-set($select)[position()>1]">
<xsl:value-of select="' '"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

