开发者

How do I access the value of this particular xml node?

I'm transforming an XML document to HTML, and wish to pick out a few particular elements (for use in the head.title of the HTML page).

My source XML looks thusly:

<document开发者_运维知识库>
  <vars>
    <var name="serialno">
      12345                     <<--- I want THIS value
    </var>
    <var name="rev-date" rev="old">
      2000-01-01
    </var>
    <var name="rev-date" rev="new">
      2011-05-01                <<--- ...and also THIS value
    </var>
    ... more vars
  </vars>
    ... more data
</document>

Now, the serial would be something along the lines of "<xsl:value-of select="/vars/var@serialno"/>", but I can't figure out how to address the var with that particular name. Similarly for the revision date, except that I need to specify two attributes. How do I do that?


<xsl:value-of select="document/vars/var[@name='serialno']"/>

AND

<xsl:value-of select="document/vars/var[@name='rev-date' and @rev='new']"/>

To test this out go to xmlper.com and key this as the XSL:

       <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- Edited by XMLSpy® -->
    <html xsl:version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns="http://www.w3.org/1999/xhtml">


     <xsl:value-of select="document/vars/var[@name='serialno']"/>

    <xsl:value-of select="document/vars/var[@name='rev-date' and @rev='new']"/>

    </html>

And this as the XML document:

<document>
  <vars>
    <var name="serialno">
      12345                    
    </var>
    <var name="rev-date" rev="old">
      2000-01-01
    </var>
    <var name="rev-date" rev="new">
      2011-05-01              
    </var>
    ... more vars
  </vars>

</document>


Use:

/*/vars/var[@name = 'serialno']/text()

to obtain the desired text node or just

string(/*/vars/var[@name = 'serialno'])

to get the desired string.

And use:

/*/vars/var
       [@name = 'rev-date' 
      and
        @rev = 'new'
       ]/text()

to obtain the 2nd desired text node or just

string(/*/vars/var
           [@name = 'rev-date' 
          and
            @rev = 'new'
           ]/text()
      )

to get its string value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜