2010-08-30 7 views
0

J'ai une question 2 parties ...XML traitement avec XSL

J'ai quelques XML qui ressemble à:

<trans-unit id="70" restype="x-text:p"> 
<source xml:lang="en-en">option 4</source> 
<target xml:lang="fr-fr">option 4</target> 
</trans-unit> 

<trans-unit id="71" restype="x-text:p"> 
<source xml:lang="en-en">option 5</source> 
<target xml:lang="fr-fr">option 5</target> 
</trans-unit> 

<trans-unit id="72" restype="x-text:p"> 
<source xml:lang="en-en">option 6</source> 
<target xml:lang="fr-fr">option 6</target> 
</trans-unit> 

Maintenant, si je veux afficher uniquement, par exemple la cible (ignorant la source), c'est un travail parfait pour XSL? Je viens d'écrire le modèle et je suis bon?

<trans-unit id="70" restype="x-text:p"> 
<target xml:lang="fr-fr">option 4</target> 
</trans-unit> 

<trans-unit id="71" restype="x-text:p"> 
<target xml:lang="fr-fr">option 5</target> 
</trans-unit> 

<trans-unit id="72" restype="x-text:p"> 
<target xml:lang="fr-fr">option 6</target> 
</trans-unit> 

et si je voulais ajouter un peu de style comme ceci:

<trans-unit id="72" restype="x-text:p"> 
<target xml:lang="fr-fr"><span class="myclass>option 6</span></target> 
</trans-unit> 

est à nouveau le XSL chemin à parcourir?

MISE À JOUR: Source XML

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="my.xsl"?> 
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"><file original="sample-document.odt/content.xml" source-language="en-en" target-language="fr-fr" datatype="x-undefined" xmlns:x="http://net.sf.okapi/ns/xliff-extensions" x:inputEncoding="UTF-8" x:configId=""> 
<body> 
<trans-unit id="1" restype="x-text:p"> 
<source xml:lang="en-en">ENGA collection of ideas about Word Processing (also a test document for Docvert)</source> 
<target xml:lang="fr-fr">FRAA collection of ideas about Word Processing (also a test document for Docvert)</target> 
</trans-unit> 
<trans-unit id="3" restype="x-text:h"> 
<source xml:lang="en-en">We Can Put an End to Word Attachments</source> 
<target xml:lang="fr-fr">We Can Put an End to Word Attachments</target> 
</trans-unit> 
<trans-unit id="5" restype="x-text:p"> 
<source xml:lang="en-en"><g id="1"><g id="2"></g></g>Don't you just hate receiving Word documents in email messages? Word attachments are annoying, but worse than that, they impede people from switching to free software. Maybe we can stop this practice with a simple collective effort. All we have to do is ask each person who sends us a Word file to reconsider that way of doing things.</source> 
<target xml:lang="fr-fr"><g id="1"><g id="2"></g></g>Don't you just hate receiving Word documents in email messages? Word attachments are annoying, but worse than that, they impede people from switching to free software. Maybe we can stop this practice with a simple collective effort. All we have to do is ask each person who sends us a Word file to reconsider that way of doing things.</target> 
</trans-unit> 
</body> 
</file> 
</xliff> 
+0

Je ne vois pas vraiment une question ici. – erikkallen

+0

s'il vous plaît, fournir un échantillon d'entrée bien formé complet. –

+0

ok, mise à jour maintenant ... – NinjaCat

Répondre

1

est un langage XSL qui transformera un dialecte XML à un autre.

Une partie de ce consiste à sélectionner les noeuds à utiliser comme source et décider de sortie - différents éléments, attributs, etc.

Ainsi, pourrait XSL être le bon choix pour vous en ce qui concerne les deux exigences. Cependant, vous pourriez trouver qu'en utilisant un analyseur XML, iterer sur des noeuds et ajouter des détails directement peut mieux vous convenir.

Il est difficile de dire à partir de l'exemple simple que vous avez donné, car les deux approches fonctionneront bien. Si vous avez besoin d'une transformation en gros, XSL est définitivement la solution.

+0

"XSL est un langage qui va transformer un dialecte XML en un autre." Bien que souvent utilisé pour cela, en utilisant XSLT vous pouvez transformer XML en un autre format, mais cela ne doit pas forcément être du XML. Il pourrait s'agir de texte brut par exemple, ou de HTML, ou même d'autre chose. – Rob

+0

@Rob - c'est vrai. Je l'ai utilisé pour transformer XML en CSV dans le passé. – Oded

1

Cette feuille de style:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="source"/> 
    <xsl:template match="target/text()[.='option 6']"> 
     <span class="myclass"> 
      <xsl:value-of select="."/> 
     </span> 
    </xsl:template> 
</xsl:stylesheet> 

Avec cette entrée:

<root> 
    <trans-unit id="70" restype="x-text:p"> 
     <source xml:lang="en-en">option 4</source> 
     <target xml:lang="fr-fr">option 4</target> 
    </trans-unit> 
    <trans-unit id="71" restype="x-text:p"> 
     <source xml:lang="en-en">option 5</source> 
     <target xml:lang="fr-fr">option 5</target> 
    </trans-unit> 
    <trans-unit id="72" restype="x-text:p"> 
     <source xml:lang="en-en">option 6</source> 
     <target xml:lang="fr-fr">option 6</target> 
    </trans-unit> 
</root> 

Sortie:

<root> 
    <trans-unit id="70" restype="x-text:p"> 
     <target xml:lang="fr-fr">option 4</target> 
    </trans-unit> 
    <trans-unit id="71" restype="x-text:p"> 
     <target xml:lang="fr-fr">option 5</target> 
    </trans-unit> 
    <trans-unit id="72" restype="x-text:p"> 
     <target xml:lang="fr-fr"> 
      <span class="myclass">option 6</span> 
     </target> 
    </trans-unit> 
</root> 
+0

J'ai mis à jour la question en réponse à cela ... – NinjaCat