2010-12-13 25 views
2

J'ai un xml comme ceci:code xsl pour obtenir une gamme de nœuds de xml

<persons> 
<person name="a"> 
</person> 
<person name="f"> 
</person> 
<person name="b"> 
</person> 
<person name="g"> 
</person> 
</persons> 

Supposons que je veux avoir tous les noeuds entre celui avec le nom « f » et celui avec le nom « g » Ainsi, l'analyse syntaxique avec xslt, il devrait produire:

<person name="b"> 
</person> 

Comment puis-je faire ??

Appréciez toute aide, merci !!

Répondre

3

Dans une liste simple (pas de groupes) cette expression XPath 1.0:

/persons/person[preceding-sibling::person[@name='f']] 
       [following-sibling::person[@name='g']] 

S'il y a des groupes, cette expression XPath 1.0:

/persons 
/person 
    [count(.|preceding-sibling::person[@name='f'][1] 
      /following-sibling::person[@name='g'][1] 
      /preceding-sibling::*) = 
    count(preceding-sibling::person[@name='f'][1] 
      /following-sibling::person[@name='g'][1] 
      /preceding-sibling::*)] 

Dans XPath 2.0, vous pouvez utiliser pour les deux cas:

/persons/person[preceding-sibling::person[@name='f'][1] 
       /following-sibling::person[@name='g'][1] >> .] 

Dans XSLT 1.0, vous pouvez utiliser:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:key name="kPersonByMarks" match="person" 
      use="concat(generate-id(
          preceding-sibling::person[@name='f'][1]), 
         '+', 
         generate-id(
          following-sibling::person[@name='g'][1]))"/> 
    <xsl:template match="person[@name='f']"> 
     <xsl:copy-of select="key('kPersonByMarks', 
           concat(generate-id(),'+', 
             generate-id(
              following-sibling::person 
                 [@name='g'][1])))"/> 
    </xsl:template> 
</xsl:stylesheet> 

Sortie:

<person name="b"></person> 

Modifier: Correct "y compris" expression XPath 1.0 et XPath 2.0

+0

+1 pour une réponse correcte. –

0

Je voudrais utiliser la fonction position() pour sélectionner les nœuds dont la position est supérieure à la position de la limite inférieure et inférieure à la position de la limite supérieure.

+0

Eh bien, ce fut ma pensée aussi mais comment est-ce fait? J'ai du mal à trouver une solution maintenant. (Bien que mon plus gros problème est que de nombreuses fonctions XPath ne sont pas trouvées dans l'implémentation Java XPath standard ...) – musiKk

+0

hmm 2 sec peut avoir une solution simple – Treemonkey

0
<xsl:apply-templates 
    match="persons/person[not(../preceding-sibling::person[f])]"/>