2016-06-02 1 views
0

Je suis actuellement, mais l'apprentissage XSLT il est encore trop flou pour moi quelque chose d'utile. Pourriez-vous s'il vous plaît me donner un indice (ou mieux, la feuille de style) pour convertir tous les éléments comme ceciXSLT, `flatmap` comme opération

<match target="font"> 
    <test name="family" compare="eq"> 
    <string>Foo</string> 
    <string>Bar</string> 
    <string>Baz</string> 
    </test> 
    <edit name="embeddedbitmap" mode="assign"><bool>false</bool></edit> 
</match> 

Dans ce cas?

<match target="font"> 
    <test name="family" compare="eq"> 
    <string>Foo</string> 
    </test> 
    <edit name="embeddedbitmap" mode="assign"><bool>false</bool></edit> 
</match> 
<match target="font"> 
    <test name="family" compare="eq"> 
    <string>Bar</string> 
    </test> 
    <edit name="embeddedbitmap" mode="assign"><bool>false</bool></edit> 
</match> 
<match target="font"> 
    <test name="family" compare="eq"> 
    <string>Baz</string> 
    </test> 
    <edit name="embeddedbitmap" mode="assign"><bool>false</bool></edit> 
</match> 

Merci.

EDIT: J'apprends et essayer d'utiliser XSLT 1.

Répondre

0

Vous n'avez pas spécifié la version XSLT que vous utilisez, donc je suppose XSLT1. En outre, votre sortie de l'échantillon manque l'élément racine, ce qui signifie qu'il n'est pas bien formé XML, donc j'ai ajouté un.

est ici une option:

<?xml version="1.0" encoding="utf-8"?> 

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs"> 

    <xsl:output method="xml" indent="yes"/> 

    <!-- Match the root node of this document --> 
    <xsl:template match="/"> 

    <!-- Create the <root> element so that the document has a root element. --> 
    <root> 
     <xsl:apply-templates/> 
    </root> 
    </xsl:template> 

    <!-- Match the <match> element. --> 
    <xsl:template match="match"> 
    <xsl:apply-templates select="test/string" mode="match"/> 
    </xsl:template> 

    <xsl:template match="string" mode="match"> 
    <!-- 
    Apply the closest ancestor <match> element of the current <string> element. 
    --> 
    <xsl:apply-templates select="ancestor::match[1]" mode="match"> 
     <!-- 
     Pass the current string element as a parameter to the matched template. 
     --> 
     <xsl:with-param name="string" select="."/> 
    </xsl:apply-templates> 
    </xsl:template> 

    <xsl:template match="match" mode="match"> 
    <!-- 
    If you're using XSLT2, you can use a tunnel parameter, in which case you 
    don't have to specify the parameter here. 
    --> 
    <xsl:param name="string"/> 

    <!-- Copy this <match> element into the output as is. --> 
    <xsl:copy> 
     <!-- Apply the attributes of this element with the default mode. --> 
     <xsl:apply-templates select="@*"/> 

     <!-- 
     Apply the child nodes (element or text) with the "match" mode. Pass the 
     <string> element we passed as a parameter earlier on to the applied 
     templates. 
     --> 
     <xsl:apply-templates select="node()" mode="match"> 
     <xsl:with-param name="string" select="$string"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="test" mode="match"> 
    <!-- 
    Remember, this parameter points to <string>Foo</string>, 
    <string>Bar</string>, or <string>Baz</string>. 
    --> 
    <xsl:param name="string"/> 

    <!-- Copy this element as is. --> 
    <xsl:copy> 
     <!-- Copy its attributes as is, too. --> 
     <xsl:apply-templates select="@*"/> 

     <!-- 
     Don't copy any of the child elements as is: rather, create a copy of the 
     <string> element we passed as a parameter here. 
     --> 
     <xsl:copy-of select="$string"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- 
    For every other attribute, element or text node that is applied with the 
    "match" mode, defer to the default mode. In this case, that means deferring 
    to the identity transform template (see below). 
    --> 
    <xsl:template match="@* | node()" mode="match"> 
    <xsl:apply-templates select="."/> 
    </xsl:template> 

    <!-- 
    Identity transform: every attribute or element matched by this template into 
    the output as is. 
    --> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

Sortie:

<root> 
    <match target="font"> 
    <test name="family" compare="eq"> 
     <string>Foo</string> 
    </test> 
    <edit name="embeddedbitmap" mode="assign"> 
     <bool>false</bool> 
    </edit> 
    </match> 
    <match target="font"> 
    <test name="family" compare="eq"> 
     <string>Bar</string> 
    </test> 
    <edit name="embeddedbitmap" mode="assign"> 
     <bool>false</bool> 
    </edit> 
    </match> 
    <match target="font"> 
    <test name="family" compare="eq"> 
     <string>Baz</string> 
    </test> 
    <edit name="embeddedbitmap" mode="assign"> 
     <bool>false</bool> 
    </edit> 
    </match> 
</root> 
+0

Merci beaucoup! Vous êtes très gentil pour inclure des commentaires détaillés. Je suis désolé mais je ne peux pas t'avoir encore. – sqxmn

+0

Pas de soucis. Heureux d'avoir pu aider. –