从属性中删除 xmlns:fm

Remove xmlns:fm from attributes

我知道这类问题已被多次回答,但从昨天开始我能找到的所有解决方案都不适合我。

我有第一个 XSL 文件,它将 "custom" XML Framemaker 文件转换为 DITA 文件。由于某些原因,我有第二个 XSL 文件,"cleans" DITA 文件获得:

<?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
  </xsl:template>

  <xsl:template match="*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/>

  <xsl:template match="//title/text()[normalize-space()]">
   <xsl:value-of select="normalize-space()"/>
  </xsl:template>

  <xsl:template match="//title/text()[not(normalize-space())]" />

  <xsl:template match="table//footnote"><fn><xsl:value-of select="."/></fn></xsl:template>

  <xsl:template match="//para"><p><xsl:value-of select="."/></p></xsl:template>

  </xsl:stylesheet>

这是输出的片段 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA 1.2 Topic//EN" "../../dtd/technicalContent/dtd/topic.dtd">
<topic id="Interfaces">
<title>Interfaces</title>
<body>
  <table xmlns:fm="fctfmns.xml" frame="all" colsep="1" rowsep="1">
     <tgroup cols="2" colsep="1" rowsep="1" outputclass="General">
        <colspec colnum="1" colname="1" colwidth="34*"/>
        <colspec colnum="2" colname="2" colwidth="66*"/>
        <thead>

但是想要的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA 1.2 Topic//EN" "../../dtd/technicalContent/dtd/topic.dtd">
<topic id="Interfaces">
<title>Interfaces</title>
<body>
  <table frame="all" colsep="1" rowsep="1">
     <tgroup cols="2" colsep="1" rowsep="1" outputclass="General">
        <colspec colnum="1" colname="1" colwidth="34*"/>
        <colspec colnum="2" colname="2" colwidth="66*"/>
        <thead>

所以我想删除 "attribute" xmlns:fm="fctfmns.xml"。但似乎我尝试包含在清洁 XSL 中的所有解决方案都是不可能的:

<xsl:template match="@xmlns:fm">

不起作用,它的衍生物也是如此(使用 name())。我还尝试在 xsl:stylesheet 中声明 xmlns:fm 并排除前缀 fm,但它也没有用。

最奇怪的是<xsl:template match="@*">删除了所有属性,但xmlns:fm没有。

编辑:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#xa0;">]>

<chapter RSID-Document = "BasisRS://OBJ_DOKU/11//001/1//11:63"
RSKM-DocuType = "Manual" RSKM-ProductType = "x"
RSKM-ProductGroup = "x"
RSKM-ProductName = "x" RSIM-Status = "3"
RSKM-Language = "EN" chaptertype = "train" ActivateFilter = "Off">
<title
id = "P_12bcc27e" startposition = "topofpage">x</title>
<module id = "P_67912cfb" xmlns:fm = "fctfmns.xml"
RSID-Component = "BasisRS://OBJ_BAUST/906/EN/001/1//925:2"
RSIM-Checksum = "4ad808b1" RSIM-XRefID = "UID:906:3:9f1e5d1">
    <topic>
        <title id = "P_37e7a2f6" RSIM-XRefID = "UID:906:1:9f1e5c1">x</title>
        <table frame = "all" colsep = "1" rowsep = "1" id = "P_982c71a0"
RSIM-XRefID = "UID:906:2:9f1e5c1">
            <tgroup cols = "2" colsep = "1" rowsep = "1" outputclass = "General">
                <colspec colnum = "1" colname = "1" colwidth = "28*"/>
                <colspec colnum = "2" colname = "2" colwidth = "72*"/>
                <thead>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                </thead>
                <tbody>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                    <row rowsep = "1">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                    <row rowsep = "0">
                        <entry colname = "1">
                            <para>x</para>
                        </entry>
                        <entry colname = "2">
                            <para>x</para>
                        </entry>
                    </row>
                </tbody>
            </tgroup>
        </table>
    </topic>
</module>
</chapter>

(上面写了想要的输出)

请注意,xmlns:fm 命名空间位于输入的模块元素中,但根据我测试的解决方案,xmlns:fm 命名空间出现在 table、主题中、标题或最常出现在 tgroup 元素中。

假设您确实使用 XSLT 2.0 处理器并且命名空间声明是从您的输入中复制的,您可以更改模板

  <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
  </xsl:template>

  <xsl:template match="node()|@*">
  <xsl:copy copy-namespaces="no">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
  </xsl:template>

请参阅 http://xsltransform.net/jyRYYhX 示例,其中输入具有命名空间声明而输出没有,因为 xsl:copy copy-namespaces="no" 尚未复制它。

通过 XSLT 1.0,您可以使用

<xsl:template match="*">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:apply-templates select="@* | node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

而不是

  <xsl:template match="node()|@*">
  <xsl:copy copy-namespaces="no">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
  </xsl:template>

您的方法的问题是您按原样复制了整个 table - 并且这也复制了当时范围内的所有命名空间。

为避免这种情况,请替换为:

<xsl:template match="table">
    <xsl:copy-of select="."/>
</xsl:template>

与:

<xsl:template match="table"> 
    <xsl:apply-templates select="." mode="remove-namespaces"/>
</xsl:template> 

<xsl:template match="*" mode="remove-namespaces">
    <xsl:element name="{local-name()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates mode="remove-namespaces"/>
    </xsl:element>
</xsl:template>

注意:这是指您在 http://xsltransform.net/ncdD7kZ 发布的样式表 - 与之前的非常 不同在你的问题中。