使用 xslt 将 XML 导入 InDesign 时出现无效命名空间错误

Invalid namespace error when importing XML into InDesign with xslt

当我尝试使用 XSLT 样式表将 XML 文件导入 InDesign 时,出现“DOM 转换错误:命名空间无效”并且导入失败。我已经尝试过 post 中提到的解决方案,但它对我不起作用:Namespace error when importing XML-file into InDesign。我一直无法找到直接处理此问题的任何其他 post。

最简单的情况下,我的代码如下所示。我已经剥离了所有其他内容,但这对我来说仍然失败。

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
    xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="book">
   <book xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
      <xsl:apply-templates/>
   </book>
</xsl:template>

</xsl:stylesheet>

顺便说一下,当我在测试环境中使用 InDesign 之外的这段代码转换 XML 时,我可以毫无问题地导入生成的 XML,这让我相信它是一个 InDesign问题而不是 XSLT 问题。我查看了我的 XSLT 以寻找可能尚未声明的另一个命名空间,但我找不到任何东西。

非常感谢任何见解!

对于将来可能遇到此问题的任何人,我能够按照位于以下位置的 XSL 文件中的插件 SDK 中的示例解决此问题:source/sdksamples/xdocbookworkflow/examplefiles

基本上,规则如下(步骤将遵循):

  1. 源 XML 不应包含任何命名空间。这些是在使用导入样式表导入期间引入的。
  2. 根元素必须包含名称空间定义。
  3. 命名空间不能直接处理,必须使用变量添加。

XSL 处理引擎对名称空间非常挑剔,即使是 Adob​​e 的 aid 和 aid5 名称空间。如果它们在您的来源 XML 中,并且您不关联导入 XSL 样式表,则可以毫无问题地导入它们。一旦添加样式表,命名空间就会中断导入并出现 'Invalid Namespace' 错误。

将命名空间添加到根元素只是一种通过创建虚拟属性的变通方法:

<xsl:template match="root-element">
<copy>
  <xsl:variable name="ns">aid</xsl:variable>
  <xsl:attribute
    name="{concat($ns, ':role')}"
    namespace="http://ns.adobe.com/AdobeInDesign/4.0/">force-namespace</xsl:attribute>
  <xsl:apply-templates/>
</copy>
</xsl:template>

aid:role 属性实际上不是一个属性,但必须用于强制命名空间正确运行。然后对于使用命名空间的每个属性,您以相同的方式构造属性:

<xsl:template match="root/title">
  <xsl:copy>
    <xsl:variable name="ns">aid</xsl:variable>
    <xsl:attribute
      name="{concat($ns, ':pstyle')}"
      namespace="http://ns.adobe.com/AdobeInDesign/4.0/">doc-title</xsl:attribute>
      <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

就我而言,我的源 XML 包含 table 单元格的宽度,因此我必须将属性名称转换为正确的辅助命名空间,以便 InDesign table 可以识别并应用正确的值。以下是我的处理方式:

<xsl:template match="@cellwidth">
  <xsl:variable name="idns">aid</xsl:variable>
  <xsl:attribute name="{concat($idns,':ccolwidth')}"
    namespace="http://ns.adobe.com/AdobeInDesign/4.0/">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>  

有关其工作原理的完整示例,请参阅上面指定的 SDK 位置中的 XSL 样式表。