użyj xpath, aby wybrać elementy z zestawem wielu atrybutów / wartości

Mam dokument XML, który muszę rozebrać poszczególne fragmenty danych

Dokument xml ma następującą strukturę:-

<a>
   <b select='yes please'>
       <c d='text1' e='text11'/>
       <c d='text2' e='text12'/>
       <c d='text3' e='text13'/>
       <c d='text4' e='text14'/>
       <c d='text5' e='text15'/>
   </b>
 </a>
<a>
   <b select='no thanks'>
       <c d='text1' e='text21'/>
       <c d='text3' e='text23'/>
       <c d='text5' e='text25'/>
   </b>
 </a>
<a>
   <b select='yes please'>
       <c d='text1' e='text31'/>
       <c d='text2' e='text32'/>
       <c d='text3' e='text33'/>
       <c d='text4' e='text34'/>
       <c d='text5' e='text35'/>
   </b>
 </a>
<a>
   <b select='no thanks'>
       <c d='text4' e='text41'/>
       <c d='text3' e='text43'/>
       <c d='text5' e='text45'/>
   </b>
 </a>

Muszę wybrać tylko te grupy elementów / a / b, które mają atrybut d = 'text1' i d atrybut = 'text4', po zidentyfikowaniu tych dokumentów podrzędnych chcę uzyskać wartość atrybutów e z wartością atrybutu d 'text5'

Hope thats clear

Cheers

DD

Author: Kirill Polishchuk, 2011-07-16

4 answers

Możesz użyć tej ścieżki XPath:

//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e

Wybierze e='text15' i e='text35' z 1. i 3. a/b

XSLT:

<xsl:template match="//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']">
  <xsl:value-of select="@e"/>
</xsl:template>
 35
Author: Kirill Polishchuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-16 16:14:05

Muszę wybrać tylko te grupy elementów /a/b, które mają atrybut d = 'text1' i atrybut d = 'text4', po zidentyfikowaniu tych dokumentów podrzędnych chcę uzyskać wartość atrybutów e z wartością atrybutu d 'text5'

Hope thats clear

Tak, to jasne, że tłumaczenie na XPath jest prawie mechaniczne

(: those /a/b element groups :) a/b 
(: that have d attribute = 'text1' :) [c/@d='text1'] 
(: and d attribute = 'text4' :) [c/@d='text4'] 
(: and .. i want to get the value of the e attributes 
   with d attribute value 'text5' :) / c[@d='text5'] / @e
 5
Author: Michael Kay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-10 23:46:27

Możesz użyć pojedynczego szablonu, aby dopasować wymaganą grupę, a następnie uzyskać wartość wymaganych atrybutów:

<xsl:template match="/*/a/b[c[@d='text1'] and c[@d='text4']]">
    <xsl:value-of select="c[@d='text5']/@e"/>
</xsl:template>

Zakładając:

<root>
    <a>
        <b select='yes please'>
            <c d='text1' e='text11'/>
            <c d='text2' e='text12'/>
            <c d='text3' e='text13'/>
            <c d='text4' e='text14'/>
            <c d='text5' e='text15'/>
        </b>
    </a>
    <a>
        <b select='no thanks'>
            <c d='text1' e='text21'/>
            <c d='text3' e='text23'/>
            <c d='text5' e='text25'/>
        </b>
    </a>
    <a>
        <b select='yes please'>
            <c d='text1' e='text31'/>
            <c d='text2' e='text32'/>
            <c d='text3' e='text33'/>
            <c d='text4' e='text34'/>
            <c d='text5' e='text35'/>
        </b>
    </a>
    <a>
        <b select='no thanks'>
            <c d='text4' e='text41'/>
            <c d='text3' e='text43'/>
            <c d='text5' e='text45'/>
        </b>
    </a>
</root>

Wyjściem będą text15 i text35.

 4
Author: Emiliano Poggi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-16 15:46:56

Użyj tego pojedynczego wyrażenia XPath :

/*/a/b[c/@d='text1' and c/@d='text4']
         /c[@d='text5']
             /@e
 1
Author: Dimitre Novatchev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-16 17:20:03