代码之家  ›  专栏  ›  技术社区  ›  NinjaCat

使用XSL处理XML

  •  0
  • NinjaCat  · 技术社区  · 15 年前

    我有一个两部分的问题…

    我有一些XML如下所示:

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

    现在,如果我只想显示,比如说目标(忽略源代码),这对于XSL来说是一个完美的工作,对吗?我只是写模板,我很好?

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

    如果我想添加这样的风格:

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

    XSL又是一种方式了?

    更新: 源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>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Oded    15 年前

    XSL是一种将一种XML方言转换为另一种XML方言的语言。

    其中一部分是选择要用作源的节点并决定输出-不同的元素、属性等。

    所以,XSL 能够 在这两个方面都是您的正确选择。

    但是,您可能会发现,使用XML解析器,迭代节点并直接添加细节可能对您更好。

    很难从您给出的简单示例中分辨出来,因为这两种方法都可以很好地工作。如果您需要大规模的转换,那么XSL绝对是一种可行的方法。

        2
  •  1
  •   user357812    15 年前

    此样式表:

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

    使用此输入:

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

    输出:

    <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>
    
    推荐文章