我在互联网上找到了很多解决这个问题的方法(也在这个论坛上),但是我仍然可以解决我的问题。我有这样的代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="#default">
<xsl:output omit-xml-declaration="yes" standalone="no" method="xml" indent="no" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tei:body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<back>
<div>
<xsl:for-each select="//tei:rs[@type='luogo']">
<p><xsl:value-of select="."/></p>
</xsl:for-each>
</div></back>
</xsl:template>
</xsl:stylesheet>除了后面的部分,输出是正确的,它重复了xmlns和xmlns:tei属性:
<back xmlns="" xmlns:tei="http://www.tei-c.org/ns/1.0"><div>....</div></back>他们已经在这里了(所以我不需要“回来”的):
<TEI xmlns="http://www.tei-c.org/ns/1.0">我试过这样的密码:
<xsl:template match="@*|node()[not(self::*)]">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>或者这个:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|*"/>
</xsl:element>
</xsl:template>
<xsl:template match="tei:back">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>我甚至修改了它以使其适合我的代码,用"tei:back“更改匹配值或select的值,它不起作用。为了使tei命名空间只出现在部分中,我必须做些什么?谢谢你的建议,谢谢你的帮助!
编辑:谢谢你的回答。我的xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="file:/C:/Users/User/Desktop/prova2.xsl"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader><fileDesc>
<titleStmt>
<title>AA</title>
</titleStmt>
<publicationStmt><p><!-- supply publication information --></p></publicationStmt>
<sourceDesc>
<bibl>AA</bibl>
</sourceDesc>
</fileDesc><profileDesc>
<langUsage>
<language ident="ita">AA</language>
<language ident="lat">AA</language>
</langUsage>
</profileDesc></teiHeader>
<text>
<body>
<div type="book" n="3" xml:id="L3">
<head>AA
</head>
<div type="cap" n="1" xml:id="L3-01">
<head>AA</head>
<p>AA
<pb n="200"/>AA
</p>
</div>
</div>
</body>
</text>
</TEI>完整的输出如下:
<?xml-stylesheet type="text/xsl" href="file:/C:/Users/User/Desktop/prova2.xsl"?><TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader><fileDesc>
<titleStmt>
<title>AA</title>
</titleStmt>
<publicationStmt><p><!-- supply publication information --></p></publicationStmt>
<sourceDesc>
<bibl>AA</bibl>
</sourceDesc>
</fileDesc><profileDesc>
<langUsage>
<language ident="ita">AA</language>
<language ident="lat">AA</language>
</langUsage>
</profileDesc></teiHeader>
<text>
<body>
<div type="book" n="3" xml:id="L3">
<head>AA
</head>
<div type="cap" n="1" xml:id="L3-01">
<head>AA</head>
<p>AA
<pb n="200"/>AA
</p>
</div>
</div>
</body><back xmlns="" xmlns:tei="http://www.tei-c.org/ns/1.0"><div/></back>
</text>
</TEI>发布于 2012-09-05 12:40:46
出现此问题是因为在输入XML文档中,所有元素都指定在命名空间"http://www.tei-c.org/ns/1.0"“中。然而,在XSLT中,您正在创建新元素(在tei:body模板中),但没有指定新元素的命名空间。这意味着新元素(例如没有命名空间创建的back ),因此输出XML包含额外的名称空间标记来表示这一点。
解决此问题的一种方法是在创建元素时指定名称空间。而不是这样做..。
<back>....</back>做这个..。
<xsl:element name="back" namespace="http://www.tei-c.org/ns/1.0">....</xsl:element>以下是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tei="http://www.tei-c.org/ns/1.0" exclude-result-prefixes="#default">
<xsl:output omit-xml-declaration="yes" standalone="no" method="xml" indent="no"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tei:body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:element name="back" namespace="http://www.tei-c.org/ns/1.0">
<xsl:element name="div" namespace="http://www.tei-c.org/ns/1.0">
<xsl:for-each select="//tei:rs[@type='luogo']">
<xsl:element name="p" namespace="http://www.tei-c.org/ns/1.0">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>当应用于示例XML时,将输出以下内容
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title>AA</title>
</titleStmt>
<publicationStmt>
<p><!-- supply publication information -->
</p>
</publicationStmt>
<sourceDesc>
<bibl>AA</bibl>
</sourceDesc>
</fileDesc>
<profileDesc>
<langUsage>
<language ident="ita">AA</language>
<language ident="lat">AA</language>
</langUsage>
</profileDesc>
</teiHeader>
<text>
<body>
<div type="book" n="3" xml:id="L3">
<head>AA </head>
<div type="cap" n="1" xml:id="L3-01">
<head>AA</head>
<p>AA
<pb n="200"/>AA
</p>
</div>
</div>
</body>
<back>
<div/>
</back>
</text>
</TEI>https://stackoverflow.com/questions/12268229
复制相似问题