2016-08-10 6 views
0

Je voudrais renommer une instruction de traitement dans xsl. Mon entrée ressemble à ceci:Renommer une instruction de traitement avec XSL

<?xml version="1.0" encoding="utf-8" ?> 
 
<material xml:lang="en-us"> 
 
    <title> 
 
    <?PI_start author="joepublic" comment="Comment #1" ?>Discovering 
 
     <?PI_end?>XML 
 
    </title> 
 
    <related-stuff> 
 
    <?PI_start author="johndoe" comment="Comment #3" ?> 
 
     <a href="otherdoc.xml" /> 
 
     <?PI_end?> 
 
    </related-stuff> 
 
</material>

Et je voudrais renommer l'instruction de traitement de « PI » à « otherPI », ainsi que de renommer l'attribut « auteur » à « utilisateur ".

Le résultat ressemblerait à ceci:

<?xml version="1.0" encoding="utf-8"?> 
 
<material xml:lang="en-us"> 
 
    <title> 
 
    <?otherPI_start user="joepublic" comment="Comment #1"?>Discovering 
 
    <?otherPI_end?>XML 
 
    </title> 
 
    <related-stuff> 
 
    <?otherPI_start user="johndoe" comment="Comment #3"?> 
 
    <a href="otherdoc.xml" /> 
 
    <?otherPI_end?> 
 
    </related-stuff> 
 
</material>

Pouvez-vous me aider à identifier la déclaration correspondante dans le xsl qui ferait cela?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
 
    <xsl:output method="xml" indent="yes" /> 
 
    <xsl:template match="node()|@*"> 
 
    <xsl:copy> 
 
     <xsl:apply-templates select="node()|@*" /> 
 
    </xsl:copy> 
 
    </xsl:template> 
 
    <xsl:template match="processing-instruction('PI_start')"> 
 
     <xsl:copy> 
 
     <!-- What should I put here??? --> 
 
     </xsl:copy> 
 
    </xsl:template> 
 
    <xsl:template match="processing-instruction('PI_end')"> 
 
     <xsl:copy> 
 
     <!-- What should I put there??? --> 
 
     </xsl:copy> 
 
    </xsl:template> 
 
</xsl:stylesheet>

Répondre

1

Pour renommer une instruction de traitement, vous feriez:

<xsl:template match="processing-instruction('PI_start')"> 
    <xsl:processing-instruction name="otherPI_start"> 
     <xsl:value-of select="." /> 
    </xsl:processing-instruction> 
</xsl:template> 

Si vous souhaitez également modifier le contenu, par exemple changer author="joepublic"-user="joepublic", vous devrez faire cette manipulation de chaîne à l'aide - par exemple:

<xsl:template match="processing-instruction('PI_start')"> 
    <xsl:processing-instruction name="otherPI_start"> 
     <xsl:value-of select="substring-before(., 'author=')" /> 
     <xsl:text>user=</xsl:text> 
     <xsl:value-of select="substring-after(., 'author=')" /> 
    </xsl:processing-instruction> 
</xsl:template> 
+0

Fonctionne parfaitement grâce. – curiousmind