开发者

XSLT - Value-Of

the same way you can do the following...

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

Can you do the same filtering with the Value-Of statement?

<xsl:value-of select="value[name='Name']" />

Thanks, james.

Edit:

Sorry for the confusion.

I had some XML:

<DynamicData>
    <item>
      <name>Name</name>
      <value xsi:type="xsd:int">0</value>
    </item>
    <item>
      <name>Value</name>
      <value xsi:type="xsd:long">9</value>
    </item>
</DynamicData>

I wanted to use a filter on my value-of select, much in the same way as is possible when doing a for-each. I've only just started looking at XSLT, so wasnt sure of its abilities. In the end i used the following XSLT:

<set>   
    <xsl:attribute name="name"> 
       <xsl:choose>
        <xsl:when test="item[name='Name']/value=0">Low</xsl:when>
        <xsl:when test="item[name='Name']/value=1">Medium</xsl:when>
        <xsl:when test="item[name='Name']/value=2">High</xsl:when>  
       </xsl:choose>
    </xsl:attribute>
    ...

The problem I was having was i was putting the filter after the value element in the test, like so. <xsl:when test="item/value[name='Name']=2">High</xsl:when> Obviously the 'name开发者_如何学编程' element isnt an element of 'value' but an element of 'item' hence why this didnt work.

Thanks for your help everyone, i got there in the end :)


Remember that in XSLT 1.0 <xsl:value-of select="someNodeSet"/> outputs only the string value of the first node in someNodeSet

On the other side:

<xsl:for-each select="someNodeSet">
 <xsl:value-of select="."/>
</xsl:for-each>

outputs the string value of every node in someNodeSet.


Yes, select takes an XPATH expression as it's argument

XSL:value-of

Whilst the specification states you can, it could depend on the implementation your XML/XSL engine.


Do note that in XSLT 1.0 you can workaround this with xsl:copy-of and text() test node.


Example, given this input:

<catalog>
    <cd>
        <artist>Bob Dylan</artist>
    </cd>
    <cd>
        <artist>Pink Floyd</artist>
        <title>ummagumma</title>
    </cd>
    <cd>
        <artist>Pink Floyd</artist>
        <title>Atom Earth Mother</title>
    </cd>
</catalog> 

You can use xsl:copy-of as follows:

 <xsl:copy-of select="catalog/cd[artist='Pink Floyd']/title/text()"/>

will return the value of all the matching nodes exactly as in:

 <xsl:for-each select="catalog/cd[artist='Pink Floyd']/title">
   <xsl:value-of select="."/>
 </xsl:for-each>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜