开发者

XPath help. Get childnode with variable name

开发者 https://www.devze.com 2023-01-02 19:55 出处:网络
I have the following XML: <StatsContainer> <Variant1>0</Variant1> <Variant2>0.5</Variant2>

I have the following XML:

<StatsContainer>
    <Variant1>0</Variant1>
    <Variant2>0.5</Variant2>
    <Variant3>1.2</Variant3>
    <Variant4>4.1</Variant4>
    <Variant5>93.9</Variant5>
    <Variant6>0.3</Variant6>
    <Variant7>0</Variant7>
    <Variant8>0</Variant8>
    <Variant9>0</Variant9>
    <Variant10>0</Variant10>
    <Variant11>0</Variant11>
    <Variant12>0</Variant12>
    <GlobalVariant1>4.6</GlobalVariant1>
    <GlobalVariant2>40.4</GlobalVariant2>
    <GlobalVariant3>13.8</GlobalVariant3>
    <GlobalVariant4>2.8</GlobalVariant4>
    <GlobalVariant5>35.6</GlobalVariant5>
    <GlobalVariant6>2.8</GlobalVariant6>
    <GlobalVariant7>0</GlobalVariant7>
    <GlobalVariant8>0</GlobalVariant8>
    <GlobalVariant9>0</GlobalVariant9>
    <GlobalVariant10>0</GlobalVariant10>
    <GlobalVariant11>0</GlobalVariant11>
    <GlobalVariant12>0</GlobalVariant12>
    <MosaicType>Boligtype</MosaicType>
    <OverRepresentedVariant>5</OverRepresentedVariant>
</StatsContainer>

As you can see, I have a number in the "OverRepresentedVariant"-tag. This number can change from time to time. What I need is to grab the "Variant"-tag with the right number. In the above case I need to get the value from the "Variant5"-tag (93.9). Tomorrow the "OverRepresentedVariant"-value might have changed to 3, this would mean that I should now grab the "Variant3"-value instead.

So this is what I got. I have a variable called $btOver which contains the above XML. I also have a variable called $btId which contains the "OverRepresentedVariant"-value like this:

<xsl:variable name=开发者_如何学C"btId" select="$btOver/OverRepresentedVariant" />

So now I need some help finding the Variant-tags with the right ID. The tags that I need will always be named "Variant" followed by an id. So how can I get the right tag?


StatsContainer/*[name() = concat('Variant', ../OverRepresentedVariant)]

PS: You really should think about changing the XML. Having data as part as the element name is a bad idea. This is how it is supposed to look like:

<StatsContainer>
  <Variant id="1">0</Variant>
  <!-- ... -->
  <Variant id="12">0</Variant>
  <GlobalVariant id="1">4.6</GlobalVariant>
  <!-- ... -->
  <GlobalVariant id="12">0</GlobalVariant>
  <MosaicType>Boligtype</MosaicType>
  <OverRepresentedVariant>5</OverRepresentedVariant>
</StatsContainer>

And the XPath would then be

StatsContainer/Variant[@id = ../OverRepresentedVariant]
0

精彩评论

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