开发者

Processing XML with XSL

开发者 https://www.devze.com 2023-01-14 07:13 出处:网络
I\'ve got a 2-part question... I have some XML that looks like: <trans-unit id=\"70\" restype=\"x-text:p\">

I've got a 2-part question...

I have some XML that looks like:

<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>

Now, if I want to only display, say the target (ignoring the source), this is a perfect job for XSL right? I just write up the template and I'm good?

<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>

and if I wanted to add some style like this:

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

XSL is again the way to go?

UPDATE: 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>


XSL is a language that will transform one XML dialect to another.

Part of that is selecting which nodes to use as source and deciding on output - different elements, attributes etc.

So, XSL could be the right choice for you in regards to both requirement.

However, you might find that using an XML parser, iterating over nodes and adding details directly may work for you better.

It is hard to tell from the simple example you gave, as both approaches will work fine. If you need wholesale transformation, XSL is definitely the way to go.


This stylesheet:

<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>

With this input:

<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>

Output:

<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

精彩评论

暂无评论...
验证码 换一张
取 消