|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello
I have a XML file which looks like this <root> <flds id="flds1"> <fld name="name1" value="value1" /> <fld name="name2" value="value2" /> <fld name="name3" value="valuename31" /> <fld name="name4" value="valuename41" /> </flds> <flds id="flds2"> <fld name="name1" value="value1" /> <fld name="name2" value="value2" /> <fld name="name3" value="valuename32" /> <fld name="name4" value="valuename42" /> </flds> <flds id="flds3"> <fld name="name1" value="valuename13" /> <fld name="name2" value="valuename23" /> <fld name="name3" value="valuename33" /> <fld name="name4" value="valuename43" /> </flds> <flds id="flds4"> <fld name="name1" value="valuename14" /> <fld name="name2" value="valuename24" /> <fld name="name3" value="valuename34" /> <fld name="name4" value="valuename44" /> </flds> </root> I want to get in a node set, ALL the <flds> containing a <fld> with attribute name1 with value = 'value1' AND a <fld> with attribute name2 with value = 'value2' How can I do that? Thanks a lot for your soon quick answer! This feature is really blocking my dev! Vero |
|
#2
|
|||
|
|||
|
Here is an example of a quick and dirty way
of doing it. There are more elegant solutions! Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:variable name="tmp" select="/root/flds[./fld[@name='name1' and @value='value1']]" />
<xsl:apply-templates select="$tmp[./fld[@name='name2' and @value='value2']]" />
</xsl:template>
<xsl:template match="/root/flds">
Node Set:
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
|
|
#3
|
||||
|
||||
|
Your xml doc has no values, but if it did, try it with this XSL:
Code:
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
doctype-public="-//W3C//DTD XHTML 1.1//EN"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>
<xsl:template match="root"><!--root-->
<html>
<body>
<xsl:for-each select = "(flds/fld[@name='name1' and @value='value1'])">
<xsl:value-of select="." />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
__________________
Hello, old friend... |
|
#4
|
|||
|
|||
|
Thanks fpmurphy for your quick answer.
First I had forgotten the "./" oups... It seems strange that we can't directly take the node set like <xsl:variable name="tmp" select="/root/flds[./fld[@name='name1' and value='value1']]" /> <xsl:value-of select="count ($tmp[./fld[@name='name2' and @value='value2']] )" /> (I need to retrive the nb of elements) Do you know why we can't do it directly? thx Vero |
|
#5
|
|||
|
|||
|
Ok, it works like this! I did a character case mistake! so the global answer is:
<xsl:variable name="nodes" select="/root/flds[./fld[@name = 'name1' and @Value = 'value1']]" /> <xsl:value-of select="count($nodes/fld[@name='name2' and @value='value2']) "/> Thanks all |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Help on Xpath expression |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|