当前位置: 首页 > 知识库问答 >
问题:

XSLT-将子节点合并为一个

呼延高超
2023-03-14
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file original="TargetInteresting1/Localizable.strings" >
        <header>
            <tool tool-id="com.apple.dt.xcode"/>
        </header>
        <body>
            <trans-unit id="Die Stadt muss mindestens 2 Buchstaben aufweisen. Die Zeichen ' und - sind erlaubt.">
                <source>Die Stadt muss mindestens 2 Buchstaben aufweisen. Die Zeichen ' und - sind erlaubt.</source>
                <target>City must contain at least 2 letters. Characters like ' and - are allowed.</target>
            </trans-unit>
            <trans-unit id="Die Standorte konnten nicht geladen werden.">
                <source>Die Standorte konnten nicht geladen werden.</source>
                <target>Could not load locations.</target>
            </trans-unit>
            <trans-unit id="Die Straße muss angegeben werden.">
                <source>Die Straße muss angegeben werden.</source>
                <target>Street is a mandatory field.</target>
            </trans-unit>
            <trans-unit id="%ix einlösebereit!">
                <source>%ix einlösebereit!</source>
                <target>%ix ready to redeem</target>
            </trans-unit>
        </body>
    </file>
    <file original="TargetInteresting2/Localizable.strings">
        <header>
            <tool tool-id="com.apple.dt.xcode"/>
        </header>
        <body>
            <trans-unit id="Die Stadt muss mindestens 2 Buchstaben aufweisen. Die Zeichen ' und - sind erlaubt.">
                <source>Die Stadt muss mindestens 2 Buchstaben aufweisen. Die Zeichen ' und - sind erlaubt.</source>
                <target>City must contain at least 2 letters. Characters like ' and - are allowed.</target>
            </trans-unit>
            <trans-unit id="Die Straße muss angegeben werden.">
                <source>Die Straße muss angegeben werden.</source>
                <target>Street is a mandatory field.</target>
            </trans-unit>
        </body>
    </file>
    <file original="TargetInteresting3/Localizable.strings">
        <header>
            <tool tool-id="com.apple.dt.xcode"/>
        </header>
        <body>
            <trans-unit id="%@ - %@">
                <source>%1$@ - %2$@</source>
                <target>%1$@ - %2$@</target>
            </trans-unit>
            <trans-unit id="Die Standorte konnten nicht geladen werden.">
                <source>Die Standorte konnten nicht geladen werden.</source>
                <target>Could not load locations.</target>
            </trans-unit>
            <trans-unit id="%ix einlösebereit!">
                <source>%ix einlösebereit!</source>
                <target>%ix ready to redeem</target>
            </trans-unit>
            <trans-unit id="Die Straße muss angegeben werden.">
                <source>Die Straße muss angegeben werden.</source>
                <target>Street is a mandatory field.</target>
            </trans-unit>
        </body>
    </file>
    <file original="TargetNotInteresting1/Localizable.strings">
        <header>
            <tool tool-id="com.apple.dt.xcode"/>
        </header>
        <body>
            <trans-unit id="%@ - %@">
                <source>%1$@ - %2$@</source>
                <target>%1$@ - %2$@</target>
            </trans-unit>
            <trans-unit id="Die Standorte konnten nicht geladen werden.">
                <source>Die Standorte konnten nicht geladen werden.</source>
                <target>Could not load locations.</target>
            </trans-unit>
        </body>
    </file>
</xliff>
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file original="TargetMerged/Localizable.strings">
        <header>
            <tool tool-id="com.apple.dt.xcode"/>
        </header>
        <body>
            <trans-unit id="Die Stadt muss mindestens 2 Buchstaben aufweisen. Die Zeichen ' und - sind erlaubt.">
                <source>Die Stadt muss mindestens 2 Buchstaben aufweisen. Die Zeichen ' und - sind erlaubt.</source>
                <target>City must contain at least 2 letters. Characters like ' and - are allowed.</target>
            </trans-unit>
            <trans-unit id="Die Standorte konnten nicht geladen werden.">
                <source>Die Standorte konnten nicht geladen werden.</source>
                <target>Could not load locations.</target>
            </trans-unit>
            <trans-unit id="Die Straße muss angegeben werden.">
                <source>Die Straße muss angegeben werden.</source>
                <target>Street is a mandatory field.</target>
            </trans-unit>
            <trans-unit id="%ix einlösebereit!">
                <source>%ix einlösebereit!</source>
                <target>%ix ready to redeem</target>
            </trans-unit>
            <trans-unit id="%@ - %@">
                <source>%1$@ - %2$@</source>
                <target>%1$@ - %2$@</target>
            </trans-unit>
        </body>
    </file>
    <file original="TargetNotInteresting1/Localizable.strings">
        <header>
            <tool tool-id="com.apple.dt.xcode"/>
        </header>
        <body>
            <trans-unit id="%@ - %@">
                <source>%1$@ - %2$@</source>
                <target>%1$@ - %2$@</target>
            </trans-unit>
            <trans-unit id="Die Standorte konnten nicht geladen werden.">
                <source>Die Standorte konnten nicht geladen werden.</source>
                <target>Could not load locations.</target>
            </trans-unit>
            <trans-unit id="%ix einlösebereit!">
                <source>%ix einlösebereit!</source>
                <target>%ix ready to redeem</target>
            </trans-unit>
            <trans-unit id="Die Straße muss angegeben werden.">
                <source>Die Straße muss angegeben werden.</source>
                <target>Street is a mandatory field.</target>
            </trans-unit>
        </body>
    </file>
    <file original="TargetNotInteresting2/Localizable.strings">
        <header>
            <tool tool-id="com.apple.dt.xcode"/>
        </header>
        <body>
            <trans-unit id="%@ - %@">
                <source>%1$@ - %2$@</source>
                <target>%1$@ - %2$@</target>
            </trans-unit>
            <trans-unit id="Die Standorte konnten nicht geladen werden.">
                <source>Die Standorte konnten nicht geladen werden.</source>
                <target>Could not load locations.</target>
            </trans-unit>
            <trans-unit id="%ix einlösebereit!">
                <source>%ix einlösebereit!</source>
                <target>%ix ready to redeem</target>
            </trans-unit>
            <trans-unit id="Die Straße muss angegeben werden.">
                <source>Die Straße muss angegeben werden.</source>
                <target>Street is a mandatory field.</target>
            </trans-unit>
        </body>
    </file>
</xliff>

不具有任何3个原始属性值(TargetInteresting1、TargetInteresting2、TargetInteresting3)的节点应不加修改地复制到最终结果中。

我正在寻找一个使用XSLT2.0的解决方案。我不知道从何说起,我还不太习惯xslt:-(

共有1个答案

谭云瀚
2023-03-14

请考虑以下最小化示例:

XML

<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file original="TargetInteresting1/Localizable.strings">
        <trans-unit>1</trans-unit>
        <trans-unit>2</trans-unit>
    </file> 
    <file original="TargetInteresting2/Localizable.strings">
        <trans-unit>3</trans-unit>
    </file> 
    <file original="TargetInteresting3/Localizable.strings">
        <trans-unit>4</trans-unit>
        <trans-unit>5</trans-unit>
        <trans-unit>6</trans-unit>
    </file> 
    <file original="TargetNotInteresting1/Localizable.strings">
        <trans-unit>7</trans-unit>
        <trans-unit>8</trans-unit>
    </file> 
    <file original="TargetNotInteresting2/Localizable.strings">
        <trans-unit>9</trans-unit>
        <trans-unit>10</trans-unit>
    </file> 
</xliff>

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="urn:oasis:names:tc:xliff:document:1.2"
xmlns="urn:oasis:names:tc:xliff:document:1.2">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/xliff">
    <xsl:copy>
        <xsl:variable name="regex">TargetInteresting[1-3]/Localizable.strings</xsl:variable>
        <file original="TargetMerged/Localizable.strings">
            <xsl:copy-of select="file[matches(@original,$regex)]/trans-unit"/>
        </file>
        <xsl:copy-of select="file[not(matches(@original,$regex))]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
   <file original="TargetMerged/Localizable.strings">
      <trans-unit>1</trans-unit>
      <trans-unit>2</trans-unit>
      <trans-unit>3</trans-unit>
      <trans-unit>4</trans-unit>
      <trans-unit>5</trans-unit>
      <trans-unit>6</trans-unit>
   </file>
   <file original="TargetNotInteresting1/Localizable.strings">
      <trans-unit>7</trans-unit>
      <trans-unit>8</trans-unit>
   </file>
   <file original="TargetNotInteresting2/Localizable.strings">
      <trans-unit>9</trans-unit>
      <trans-unit>10</trans-unit>
   </file>
</xliff>
 类似资料:
  • 我有一个XML结构,我想用XSLT转换它。然而,使其尽可能动态化是很重要的。我相信制作一个feed的副本是可能的,然后只需选择一个特定的部分并对其进行转换。当一个新节点添加到原始XML中时,不需要在XSLT中进行任何更改,就可以将这个新节点包含在XSLT的输出中。 原始XML示例:

  • 我想从父节点复制到子节点。我真的不确定这是如何实现的。 我的源xml 我想得到输出为 我想要XSLT1.0中的解决方案。 我想将这些节点复制到子节点 谢谢。

  • 我想用XSLT转换XML文档。按名称和属性匹配的节点及其子节点应嵌套/移动到新节点中。 从转变 到 但是我的XSLT工作不好。您有什么提示吗,XSLT文件有什么问题? 非常感谢你 安德烈亚斯 我的XSLT 输出: 我的源XML文件 我希望在转换后的输出中将v:data节点及其子节点嵌套到一个新节点中。但只有属性名为“Custon”的v:data节点。转换后的XML文档应该如下所示 如果我从XSLT

  • 我需要将子元素复制到父元素中。 输入 期望输出 我尝试的内容(输出与输入保持相同): 我肯定会错过一些非常简单的事情。子元素与父元素具有相同的名称,这应该不是问题?

  • 我需要以下xslt代码方面的帮助。我的意见如下: 我的预期输出为: 我曾尝试使用*/text()获取值节点的值,但我只从第一个孩子获得文本。将来我有很多这样的子元素。 提前谢谢。 你好,Minakshi