XSLT - Value-Of

2019-09-01 02:09发布

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 :)

标签: xslt
3条回答
做自己的国王
2楼-- · 2019-09-01 02:44

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-09-01 02:56

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.

查看更多
唯我独甜
4楼-- · 2019-09-01 03:04

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>
查看更多
登录 后发表回答