gotmike
gotmike

Reputation: 1615

XPath using Predicate starts-with but looking for sister value

I have seem lots of examples of XPath predicate starts-with that show that as the final filter, but nothing that uses the starts-with predicate to filter and then return a sister value.

For instance, here is sample XML:

<myXML>
  <InputAssumptions>
    <Data>
      <General>
        <GeneralTable>
          <GeneralMethod>PGR</GeneralMethod>
          <vData>
            <vEntry>
              <EffectiveDate RelativeTo="Absolute">2015-06-01</EffectiveDate>
              <Percentage>0.01</Percentage>
            </vEntry>
            <vEntry>
              <EffectiveDate RelativeTo="Absolute">2016-06-01</EffectiveDate>
              <Percentage>0.02</Percentage>
            </vEntry>
            <vEntry>
              <EffectiveDate RelativeTo="Absolute">2017-06-01</EffectiveDate>
              <Percentage>0.03</Percentage>
            </vEntry>
            <vEntry>
              <EffectiveDate RelativeTo="Absolute">2018-06-01</EffectiveDate>
              <Percentage>0.04</Percentage>
            </vEntry>
            <vEntry>
              <EffectiveDate RelativeTo="Absolute">2019-06-01</EffectiveDate>
              <Percentage>0.05</Percentage>
            </vEntry>
          </vData>
        </GeneralTable>
      </General>
    </Data>
  </InputAssumptions>
</myXML>

What I want is to be able to supply a YEAR and return the Percentage for that year. The data is produced in such a way that there will only ever be one entry for each year. So simply returning the first entry is fine.

So, I can return the first DATE which starts with "2016" (for example) using the following XPath:

/myXML/InputAssumptions/Data/General/GeneralTable/vData/vEntry/EffectiveDate[starts-with(.,"2016")]

this returns:

<EffectiveDate RelativeTo="Absolute">2016-06-01</EffectiveDate>

And... I can return the the first PERCENTAGE which exactly matches "2016-06-01" using the following XPath:

/myXML/InputAssumptions/Data/General/GeneralTable/vData/vEntry[EffectiveDate="2016-06-01"]/Percentage

this returns:

<Percentage>0.02</Percentage>

However, I CANNOT seem to figure out how to combine the two and show the corresponding PERCENTAGE using the starts-with predicate function applied to the sister entry of EffectiveDate.

I imagine this is possible, it seems very simple, but I can't find it mentioned anywhere, or at least can't figure out how to search for it...

Any help?

Upvotes: 0

Views: 90

Answers (2)

Michael Kay
Michael Kay

Reputation: 163665

You're so close! Just replace the equality test with a starts-with test:

/myXML/InputAssumptions/Data/General/GeneralTable/
vData/vEntry[starts-with(EffectiveDate,"2016-06-01")]/Percentage

Upvotes: 2

Kachna
Kachna

Reputation: 2961

Try it this way:

//vEntry[EffectiveDate[starts-with(.,  '2016')]]/Percentage

It selects <Percentage> elements that are children of <vEntry> elements that also contain at least one <EffectiveDate> element child that starts with 2016.

Upvotes: 0

Related Questions