命名空间防止变换
我运行以下变换:命名空间防止变换
java -jar saxon9.jar -it:main -xsl:my.xsl dir="ca-ES"
的“CA-ES”包含具有下列根元素XLIFF文件:
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cba="http://www.softcon.de/XML-schema/de.softcon.cba.itembuilder.xliff-supplement"
version="1.1"
xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.1 xliff-core-1.1.xsd">
变换从使用相同的文件辅助输入文件夹并生成与输入文件夹中找到的许多结构相同的文件。
如果我从根目录(即xliff
)中删除第一个名称空间(即xmlns="urn:oasis:names:tc:xliff:document:1.1"
),那么转换就像一个魅力。但是,如果我保留它,那么它不起作用。
不工作的部分是,在主输入文件匹配source
,并从二次输入文件与source
替换它)的模板:
<xsl:copy-of select="key('ref', ../@id, doc($secondary-input))/source" />
如果不工作,我的意思是,source
从主要输入文件位于输出中,而不是来自辅助输入的预期source
节点。所以看起来命名空间正在阻碍匹配。
寻找我已经尝试了样式表的文档元素添加xpath-default-namespace
属性的解决方案:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="urn:oasis:names:tc:xliff:document:1.1"
version="2.0">
但随后从主输入source
节点保持在输出(即,它不被替换与二次输入的一个)。
我自己也尝试加入这个前缀,然后用它在样式表来匹配节点(例如match="xlf:source"
),但后来没有source
节点在所有输出:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlf="urn:oasis:names:tc:xliff:document:1.1"
version="2.0">
我会感谢一些提示。
我使用的是XSLT 2.0和saxonb9-1-0-8j。
UPDATE
添加一个主输入文件样品(命名为ca-ES_blabla.xlf
,从文件夹取CA-ES):
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cba="http://www.softcon.de/XML-schema/de.softcon.cba.itembuilder.xliff-supplement" version="1.1" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.1 xliff-core-1.1.xsd">
<file datatype="html" original="project.properties" source-language="ca">
<body>
<trans-unit id="MDSD_0" xml:space="default">
<source>Gestiona les adreces d'interès</source>
<target>Gestiona les adreces d'interès</target>
<context-group name="era">
<context context-type="x-property-id">bookmarkHeaderText</context>
</context-group>
</trans-unit>
<trans-unit id="7" xml:space="default">
<source><b>Sempre rebrà un avís quan arribi a un punt a partir del qual no pugui tornar enrere.</b><br /></source>
<target><b>Sempre rebrà un avís quan arribi a un punt a partir del qual no pugui tornar enrere.</b><br /></target>
<prop-group name="item_description">
<prop prop-type="x-inquiry-nr">4</prop>
<prop prop-type="x-type">question</prop>
</prop-group>
</trans-unit>
</body>
</file>
<!-- the xliff document might contain several <file> nodes -->
</xliff>
一个次级输入文件样品(命名为en-GB_blabla.xlf
,取自文件夹en-GB):
<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cba="http://www.softcon.de/XML-schema/de.softcon.cba.itembuilder.xliff-supplement" version="1.1" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.1 xliff-core-1.1.xsd">
<file datatype="html" original="project.properties" source-language="en-GB">
<body>
<trans-unit id="MDSD_0" xml:space="default">
<source>Manage your bookmarks</source>
<target>Manage your bookmarks</target>
<context-group name="era">
<context context-type="x-property-id">bookmarkHeaderText</context>
</context-group>
</trans-unit>
<trans-unit id="7" xml:space="default">
<source><b>You will always receive a warning before reaching a point where you cannot go back.</b><br /></source>
<target><b>You will always receive a warning before reaching a point where you cannot go back.</b><br /></target>
<prop-group name="item_description">
<prop prop-type="x-inquiry-nr">4</prop>
<prop prop-type="x-type">question</prop>
</prop-group>
</trans-unit>
</body>
</file>
<!-- the xliff document might contain several <file> nodes -->
</xliff>
和样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="urn:oasis:names:tc:xliff:document:1.1"
version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- run as:
$> java -jar saxon9.jar -it:main -xsl:this_stylesheet.xsl dir="xx-XX" -->
<!-- this captures the folder parameter given in the call -->
<xsl:param name="dir" select="dir" />
<!-- this template iterates through the files in the input folder -->
<xsl:template name="main">
<xsl:variable name="input-files" select="concat($dir, '?select=*.xlf')" />
<xsl:apply-templates select="collection($input-files)"/>
</xsl:template>
<!-- this template defines the name of the output folder and files -->
<xsl:template match="/">
<xsl:variable name="output-name" select="replace(
tokenize(document-uri(/), '/')[last()],
'(.+)\.xlf',
'$1_bilingual.xlf'
)"/>
<xsl:result-document href="{$dir}_output/{$output-name}">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:template>
<!-- this template fetches the source from the English files -->
<xsl:key name="ref" match="trans-unit" use="@id"/>
<xsl:template match="source">
<xsl:variable name="input-uri" select="document-uri(/)" />
<!-- <xsl:message><xsl:value-of select="$input-uri" /></xsl:message> -->
<xsl:variable name="secondary-input" select="replace($input-uri, $dir, 'en-GB')"/>
<xsl:copy-of select="key('ref', ../@id, doc($secondary-input))/source" />
</xsl:template>
<!-- this part generates the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这似乎是一个命名空间的问题,当我从源拷贝过来的值"urn:oasis:names:tc:xliff:document:1.1"
为xpath-default-namespace="urn:oasis:names:tc:xliff:document:1.1"
代码工作的样式表。
我不知道你在你的样式代码字符(氧气问我粘贴代码时启用一些语言支持),但不知何故字符串
"urn:oasis:names:tc:xliff:document:1.1"
"urn:oasis:names:tc:xliff:document:1.1"
不相等:
var s1 = "urn:oasis:names:tc:xliff:document:1.1";
var s2 = "urn:oasis:names:tc:xliff:document:1.1";
document.writeln('<code>' + s1 + ' === ' + s2 + ' : ' + (s1 === s2) + '<\/code>');
这可能是在样式表的命名空间包含一个或多个https://en.wikipedia.org/wiki/Zero-width_non-joiner之后的第一个数字1
。
准确地说,我的xmlns字符串有一个零宽度非连接器(U + 200C)和一个零宽度空间(U + 200B)。说实话,我不知道它们来自哪里,但你是对的,删除样式表工作的无关字符。谢谢,马丁。 – msoutopico
做两个输入文件(主要拉在收集和二次与文档被拉)使用完全相同的命名空间? –
嗨马丁:)是的,主要和次要输入文件是相同的,除了'源'节点的内容。 – msoutopico
您可能需要添加样式表和两个输入示例,以便我们在您自己找不到问题时进行调试,我认为它必须是两个文件之间的命名空间差异。 –