Ich habe ein XML-Dokument, mit dem ich bestimmte Datenelemente entfernen kann
das XML-Dokument hat folgende 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>
ich muss nur die/a/b-Elementgruppen auswählen, die d attribute = 'text1' und d attribute = 'text4' haben. Sobald ich diese Unterdokumente identifiziert habe, möchte ich den Wert der e-Attribute mit d attribute value erhalten. text5 '
hoffe das ist klar
Prost
DD
Sie können diesen XPath verwenden:
//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e
Es wird e='text15'
Und e='text35'
Von 1. und 3. a/b
Ausgewählt.
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>
ich muss nur die/a/b-Elementgruppen auswählen, die d attribute = 'text1' und d attribute = 'text4' haben. Sobald ich diese Unterdokumente identifiziert habe, möchte ich den Wert der e-Attribute mit d attribute value erhalten. text5 '
hoffe das ist klar
Ja, es ist so klar, dass die Übersetzung in XPath fast mechanisch ist
(: 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
Sie können eine einzelne Vorlage verwenden, um der erforderlichen Gruppe zu entsprechen. Anschließend erhalten Sie den Wert der erforderlichen Attribute:
<xsl:template match="/*/a/b[c[@d='text1'] and c[@d='text4']]">
<xsl:value-of select="c[@d='text5']/@e"/>
</xsl:template>
vorausgesetzt:
<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>
die Ausgabe ist text15
und text35
.
Verwenden Sie diesen einzelnen XPath-Ausdruck:
/*/a/b[c/@d='text1' and c/@d='text4']
/c[@d='text5']
/@e