-1

我在 xslt 转换中遇到了一些奇怪的问题,这可能是一个真正的问题,或者可能只是我忘记了一些东西。任何附加了 xsl:apply-templates 的东西都会导致空白,我不明白为什么。

我使用的xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<TEI.2>
    <teiHeader>
        <fileDesc>
            <titleStmt>
                <title>William of Palerne: An Electronic Edition</title>
                <author>Anonymous</author>
                <editor>Edited by G. H. V. Bunt</editor>
                <respStmt>
                    <resp>
                        <hi rend="bold">Computer Consultants and Programmers</hi>
                    </resp>
                    <name> Susan Gants, Susan Munson, Daniel Pitti, and John Unsworth.</name>
                </respStmt>
            </titleStmt>
        </fileDesc>
    </teiHeader>
</TEI>

我申请的xslt如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="xs tei"
    version="2.0">
    <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>

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

    <xsl:template match="TEI.2">
        <TEI xmlns="http://www.tei-c.org/ns/1.0">
            <xsl:apply-templates/>
        </TEI>
    </xsl:template>

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

我希望我的结果是 TEI.2 转换为 TEI 并添加了命名空间的原始 XML:

<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <teiHeader>
        <fileDesc>
            <titleStmt>
                <title>William of Palerne: An Electronic Edition</title>
                <author>Anonymous</author>
                <editor>Edited by G. H. V. Bunt</editor>
                <respStmt>
                    <resp>
                        <hi rend="bold">Computer Consultants and Programmers</hi>
                    </resp>
                    <name> Susan Gants, Susan Munson, Daniel Pitti, and John Unsworth.</name>
                </respStmt>
            </titleStmt>
        </fileDesc>
    </teiHeader>
</TEI>

相反,TEI.2 按预期更改为 TEI,但 teiHeader 未出现:

<TEI xmlns="http://www.tei-c.org/ns/1.0">

        <fileDesc xmlns="">
            <titleStmt>
                <title>William of Palerne: An Electronic Edition</title>
                <author>Anonymous</author>
                <editor>Edited by G. H. V. Bunt</editor>
                <respStmt>
                    <resp>
                        <hi>Computer Consultants and Programmers</hi>
                    </resp>
                    <name> Susan Gants, Susan Munson, Daniel Pitti, and John Unsworth.</name>
                </respStmt>
            </titleStmt>
        </fileDesc>

</TEI>

我确定我犯了错误或忽略了某些东西,但我终其一生都无法弄清楚它是什么。如果有人能告诉我什么让我搞砸了以及如何纠正它,我将不胜感激。

4

1 回答 1

1

由于teiHeader此模板,没有出现:

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

您正在匹配teiHeader,但一旦匹配,您就不会复制它,而是将控制权传递给它的子节点,导致teiHeader输出中没有写入。

现在,您可以简单地删除此模板,然后teiHeader将创建。但是,您会看到它会像这样输出

<teiHeader xmlns="">

这是因为在输入 XMLteiHeader中不属于任何命名空间,因此标识模板也只是在没有命名空间的输出中创建相同的元素。

即使您TEI在命名空间中创建根元素,这也不会自动将您创建的任何子元素添加到该命名空间。您需要为此添加单独的模板。

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="xs tei"
    version="2.0">
    <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="no"/>

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

    <xsl:template match="TEI.2">
        <TEI xmlns="http://www.tei-c.org/ns/1.0">
            <xsl:apply-templates/>
        </TEI>
    </xsl:template>

    <xsl:template match="*" priority="2">
        <xsl:element name="{local-name()}" namespace="http://www.tei-c.org/ns/1.0">
            <xsl:apply-templates select="node()|@*" />
        </xsl:element>
    </xsl:template> 
</xsl:stylesheet>

priority="2"用于确保模板获得比身份模板更高的优先级。

还要注意它是如何做的<xsl:apply-templates select="node()|@*" />,而不是<xsl:apply-templates />因为后者只选择节点,而不是 attibrutes。

于 2015-06-01T18:44:33.887 回答