我需要使用XSLT 1.0在XML中添加命名空间。我在XSLT中收到以下错误-错误:“不受支持的XSL元素”http://www.w3.org/1999/XSL/Transform:命名空间”
输入XML-
<?xml version="1.0" encoding="UTF-8"?>
<ns0:NAXML-POSJournal version="" xmlns:ns0="http://www.naxml.org/POSBO/Vocabulary/2003-10-16">
<ns0:TransmissionHeader>
<ns0:StoreLocationID/>
<ns0:VendorName/>
<ns0:VendorModelVersion/>
<ns1:TransmissionHeaderExtension xmlns:ns1="http://www.radiantsystems.com/NAXML-Extension" />
</ns0:TransmissionHeader>
<ns0:JournalReport>
<ns0:JournalHeader>
<ns0:ReportSequenceNumber/>
<ns0:PrimaryReportPeriod/>
<ns0:SecondaryReportPeriod/>
<ns0:BeginDate/>
<ns0:BeginTime/>
<ns0:EndDate/>
<ns0:EndTime/>
</ns0:JournalHeader>
</ns0:JournalReport>
</ns0:NAXML-POSJournal>
XSLT -
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.radiantsystems.com/NAXML-Extension" xmlns:ns0="http://www.naxml.org/POSBO/Vocabulary/2003-10-16" exclude-result-prefixes="ns0 ns1" version="3.4">
<xsl:output encoding='UTF-8' indent='yes' method='xml' />
<xsl:template match="*" priority="1" version="3.4">
<xsl:element name="{local-name()}" namespace="http://www.naxml.org/POSBO/Vocabulary/2003-10-16">
<xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'" />
<xsl:namespace name="xmime" select="'http://www.w3.org/2005/05/xmlmime'" />
<xsl:namespace name="radiant" select="'http://www.radiantsystems.com/NAXML-Extension'" />
<xsl:namespace name="schemaLocation" select="'http://www.naxml.org/POSBO/Vocabulary/2003-10-16 NAXML-PBI34RadiantExtended.xsd'" />
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
</xsl:transform>
输出(必填)-
<?xml version="1.0" encoding="UTF-8"?>
<NAXML-POSJournal xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:schemaLocation="http://www.w3.org/2005/05/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:radiant="http://www.radiantsystems.com/NAXML-Extension" xmlns="http://www.naxml.org/POSBO/Vocabulary/2003-10-16"
version="">
<TransmissionHeader>
<StoreLocationID/>
<VendorName/>
<VendorModelVersion/>
<TransmissionHeaderExtension/>
</TransmissionHeader>
<JournalReport>
<JournalHeader>
<ReportSequenceNumber/>
<PrimaryReportPeriod/>
<SecondaryReportPeriod/>
<BeginDate/>
<BeginTime/>
<EndDate/>
<EndTime/>
</JournalHeader>
</JournalReport>
</NAXML-POSJournal>
我尝试了另一个XSLT映射,它删除了所有命名空间,但逻辑上应该添加命名空间。
XSLT-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="current()"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | * | text()"/>
</xsl:element>
</xsl:template>
<xsl:template match="text()">
<xsl:copy>
<xsl:value-of select="current()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/NAXML-POSJournal">
<NAXML-POSJournal xmlns="http://www.naxml.org/POSBO/Vocabulary/2003-10-16"
xmlns:radiant="http://www.radiantsystems.com/NAXML-Extension"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
xmlns:schemaLocation="http://www.w3.org/2005/05/test">
<xsl:apply-templates select="@*|node()"/>
</NAXML-POSJournal>
</xsl:template>
</xsl:stylesheet
如果您知道根元素的名称,那么您可以简单地执行:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="http://www.naxml.org/POSBO/Vocabulary/2003-10-16">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/*">
<NAXML-POSJournal xmlns="http://www.naxml.org/POSBO/Vocabulary/2003-10-16" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:schemaLocation="http://www.w3.org/2005/05/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:radiant="http://www.radiantsystems.com/NAXML-Extension">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</NAXML-POSJournal>
</xsl:template>
</xsl:stylesheet>
否则会变得更复杂。但是你的第二次尝试表明你有。