XSLT具有不同的结束标记

时间:2015-11-23 08:30:59

标签: xml xslt

这是我的xml文件,我只想获取“off”值,但结束标记不一样。我尝试过外卡,但它不起作用。有任何想法吗??感谢

<?xml version="1.0" encoding="UTF-8"?>
<openremote xmlns="http://www.openremote.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openremote.org http://www.openremote.org/schemas/controller.xsd">
 <status id="5572173">off</status>
</openremote>

这是我到目前为止尝试过的xslt:

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

   <xsl:template match="/">
       <xsl:value-of select="*/status id="5572173"> 
   </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

如果您想获取属性的值,请使用前面的@前缀...

<xsl:value-of select="*/status/@id" /> 

这会得到@id的值。如果您想获取status为特定值的id元素,请执行此操作(使用方括号表示过滤status元素选择的条件。)

<xsl:value-of select="*/status[@id='5572173']" /> 

然而......你的XML中有一个命名空间声明(由xmlns表示),使问题复杂化......

<openremote xmlns="http://www.openremote.org" ..

xmlns不是属性,而是声明元素和后代所属的默认命名空间。这意味着名称空间中名为status的元素与不在名称空间中的名为status的元素不同。您的XSLT需要考虑到这一点,因为目前它正在寻找没有命名空间的元素。

试试这个XSLT ....

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

    <xsl:template match="/">
       <xsl:value-of select="*/o:status[@id='5572173']" />
    </xsl:template>
</xsl:stylesheet>

请注意,前缀o的使用是任意的。这是名称空间URI(在这种情况下为“http://www.openremote.org”)必须匹配。

http://www.xml.com/pub/a/1999/01/namespaces.htmlhttp://www.xml.com/pub/a/2001/04/04/trxml/上阅读名称空间(例如)