I have this input:
<?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>
and I need this output:
<?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>
It needs to do the following:
create a new <file>
node: <file original="TargetMerged/Localizable.strings"...>
and move all <trans-unit>
nodes from <file original = "TargetInteresting1/Localizable.strings"
, <file original = "TargetInteresting2/Localizable.strings"
and <file original = "TargetInteresting3/Localizable.strings"
into that merged node (if it's not already there, I guess this can be checked using the id
attribute of <trans-unit>
).
Nodes, that do NOT have any of the 3 original
attribute values (TargetInteresting1, TargetInteresting2, TargetInteresting3) should be copied into the final result unmodified.
I am looking for a solution with xslt 2.0. I don't know where to start, I'm not really used to xslt :-(
Consider the following minimized example:
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>
Result
<?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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.