2009-10-12 11 views
1

i ont le code XML suivant, qui est le résultat de runnig xslt:XSLT - activer la fonction XPath dans XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<toc xmlns:fn="http://www.w3.org/2004/07/xpath-functions" label="Sample Table of Contents"> 
    <topic label="Title1" href="Ref1#ref1"> 
     <topic label="Title 2" href="Ref2#ref2"> 
      <topic label="Title3" href="Ref3#ref3"/> 
      <topic label="Title4" href="Ref4#ref4"/> 
     </topic> 
     <topic label="Title5" href="Ref5#ref5"/> 
    </topic> 
    <topic label="Title6" href="Ref6#ref6"/> 
</toc> 

et XSLT suivante qui produit ce xml:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="D:\Documents and Settings\oshecht\Desktop\XSL\Copy of toc.xml"?> 
<?altova_samplexml D:\Documents and Settings\oshecht\Desktop\XSL\Copy of toc.xml?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2004/07/xpath-functions"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:template match="BODY"> 
     <toc label="Sample Table of Contents"> 
      <xsl:apply-templates select="UL/LI/OBJECT"/> 
     </toc> 
    </xsl:template> 
    <xsl:template match="OBJECT"> 
     <topic label="{param[@name='Name']/@value}" href="{param[@name='Local']/@value}"> 
      <xsl:apply-templates select="following-sibling::UL/LI/OBJECT"/> 
     </topic> 
    </xsl:template> 
</xsl:stylesheet> 

je veux que dans le fichier XML de sortie, je vais avoir dans:

follwing le:

- la même ligne, mais au lieu de href = "Ref1 # ref1" Souhaitée:

href = "Ref1" - éliminer tout après "#"

je sais au sujet de la sous-chaîne-avant que la fonction (Ref1 # ref1, '#') mais comment puis-je l'activer depuis mon XSLT?

pouvez-vous s'il vous plaît aviser?

+1

cela a été fait résolu dans les commentaires à votre question initiale - pourquoi demander à nouveau? http://stackoverflow.com/questions/1553575/xslt-how-to-parse-xml-with-recursive-elements-to-eclipse-toc-xml/1553615#1553615 – Tomalak

+0

J'ai ajouté une question ci-dessous la réponse ... la syntaxe n'a pas fonctionné, je un hetting "xpath invalide" – orly

Répondre

0

Vous pouvez envelopper l'expression XPath sélection du @value à l'intérieur du modèle de valeur d'attribut pour le @href, ce qui donnera le résultat de l'appel de fonction de la valeur @href:

<xsl:template match="OBJECT"> 
    <topic label="{param[@name='Name']/@value}" href="{substring-before(param[@name='Local']/@value, '#')}"> 
      <xsl:apply-templates select="following-sibling::UL/LI/OBJECT"/> 
    </topic> 
</xsl:template> 

Si la valeur d'attribut modèle est source de confusion, vous pouvez aussi briser le @href attribut de cette façon:

<xsl:template match="OBJECT"> 
    <topic label="{param[@name='Name']/@value}"> 
      <xsl:attribute name="href"> 
       <xsl:value-of select="substring-before(param[@name='Local']/@value, '#')" /> 
      </xsl:attribute> 
      <xsl:apply-templates select="following-sibling::UL/LI/OBJECT"/> 
    </topic> 
</xsl:template> 
+0

Merci vous :) cela a fonctionné. – orly