从属性

时间:2015-10-21 13:41:54

标签: xml xslt xml-namespaces

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

我有第一个XSL文件,它将“自定义”XML Framemaker文件转换为DITA文件。出于某些原因,我有第二个XSL文件“清理”获得的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>

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

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

不起作用,其衍生物(名称())也不起作用。我还尝试在xmlns:fm中声明xsl:stylesheet并排除前缀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命名空间位于输入的module元素中,但根据我测试的解决方案,xmlns:fm命名空间出现在表,主题,标题中,或者最常出现在tgroup元素中

2 个答案:

答案 0 :(得分:1)

假设您确实使用了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>

答案 1 :(得分:1)

您的方法的问题在于您按原样复制整个表 - 这也会复制此时范围内的所有名称空间。

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

<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发布的样式表 - 非常与您问题中的样式表不同。

相关问题