Xpath expression to find values that start with

37,877

I think this xpath should work //id[starts-with(text(),'Annotations')]

Share:
37,877
Will
Author by

Will

Updated on April 20, 2020

Comments

  • Will
    Will about 4 years

    I need an Xpath expression that will select values that start with a certain value. For this instance I am using the id field.

    @id=[starts-with(name(),'value')
    

    The following does not work. Is there a way to use the starts-with command with the value between the tags? Or is there another means in xpath of selecting a matching a value with a known value.

    Here is a sample of the xml I am trying to drill into:

        <bean>
    <id>AnnotationsBasedJMXAutoExporter</id>
    <class>org.springframework.jmx.export.MBeanExporter</class>
    <lazy-init>false</lazy-init>
            <property>assembler
                    <!-- will create management interface using annotation metadata -->
        <bean>
    
  • Ian Roberts
    Ian Roberts about 11 years
    +1, though I'd recommend using . rather than text(), i.e. //id[starts-with(., 'whatever')]. As a general rule you should avoid using text() in XPath expressions unless you are really sure that you need it. Remember that it means "the set of all text node children of ..." (or "the first text node child of ..." if you're treating it as a string), which is not necessarily the same as "the text value of ..."