我正在接收包含适当数据的XML文件。 XML的接收者不需要很少的XML命名空间,我不知道为什么。
应该添加很少的新命名空间。
我输入文件为
<Document xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://rep.evenex.dk/schema/evenex/eBusiness_01 http://rep.evenex.dk/schema/evenex/eBusiness_01/e-commerce_101.xsd"
System="HighJump"
Version="101">
<Header>
<SenderEndpointID qualifier="EAN">98989898</SenderEndpointID>
<ReceiverEndpointID qualifier="EAN">98989898</ReceiverEndpointID>
<CreatedDate>13-06-2017</CreatedDate>
<CreatedTime>10:18:00</CreatedTime>
<EDIRefNo>6136</EDIRefNo>
<Test>false</Test>
<AcknowledgementRequest>No</AcknowledgementRequest>
</Header>
<Body>
<DispatchReference>6136</DispatchReference>
<DocumentType>ORDERS</DocumentType>
<DocumentNo>98989898</DocumentNo>
<DocumentDate>13-06-2017</DocumentDate>
<RequestedDeliveryDate>19-06-2017</RequestedDeliveryDate>
<CurrencyCode>DKK</CurrencyCode>
<ExternalDocumentNo>100718360</ExternalDocumentNo>
<ShipmentMethodCode>DAP</ShipmentMethodCode>
<Parties>
<Party type="Sellto">
<No>98989898</No>
</Party>
<Party type="StoreNumber">
<No>98989898</No>
</Party>
<Party type="Supplier">
<No>98989898</No>
</Party>
</Parties>
<Lines>
<Line>
<LineNo>1</LineNo>
<DocumentNo>100718360</DocumentNo>
<EANNo>98989898</EANNo>
<Quantity>5</Quantity>
<UnitofMeasure>PCE</UnitofMeasure>
<UnitPrice>166.91</UnitPrice>
</Line>
<Line>
<LineNo>2</LineNo>
<DocumentNo>100718360</DocumentNo>
<EANNo>98989898</EANNo>
<Quantity>10</Quantity>
<UnitofMeasure>PCE</UnitofMeasure>
<UnitPrice>166.91</UnitPrice>
</Line>
</Lines>
</Body>
</Document>
我需要删除一些名称空间并添加一些新的名称空间。 输出应该是
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SystemVersion="7.00.3.71.03" System="HighJump" Version="101">
<Header>
<SenderEndpointID qualifier="EAN">98989898</SenderEndpointID>
<ReceiverEndpointID qualifier="EAN">98989898</ReceiverEndpointID>
<CreatedDate>13-06-2017</CreatedDate>
<CreatedTime>10:18:00</CreatedTime>
<EDIRefNo>6136</EDIRefNo>
<Test>false</Test>
<AcknowledgementRequest>No</AcknowledgementRequest>
</Header>
<Body>
<DispatchReference>6136</DispatchReference>
<DocumentType>ORDERS</DocumentType>
<DocumentNo>98989898</DocumentNo>
<DocumentDate>13-06-2017</DocumentDate>
<RequestedDeliveryDate>19-06-2017</RequestedDeliveryDate>
<CurrencyCode>DKK</CurrencyCode>
<ExternalDocumentNo>100718360</ExternalDocumentNo>
<ShipmentMethodCode>DAP</ShipmentMethodCode>
<Parties>
<Party type="Sellto">
<No>98989898</No>
</Party>
<Party type="StoreNumber">
<No>98989898</No>
</Party>
<Party type="Supplier">
<No>98989898</No>
</Party>
</Parties>
<Lines>
<Line>
<LineNo>1</LineNo>
<DocumentNo>100718360</DocumentNo>
<EANNo>98989898</EANNo>
<Quantity>5</Quantity>
<UnitofMeasure>PCE</UnitofMeasure>
<UnitPrice>166.91</UnitPrice>
</Line>
<Line>
<LineNo>2</LineNo>
<DocumentNo>100718360</DocumentNo>
<EANNo>98989898</EANNo>
<Quantity>10</Quantity>
<UnitofMeasure>PCE</UnitofMeasure>
<UnitPrice>166.91</UnitPrice>
</Line>
</Lines>
</Body>
</Document>
我正在尝试使用XSLT脚本先删除然后再添加。但它不起作用
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
>
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<!--Identity template, provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我想删除以下命名空间
xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01"
xsi:schemaLocation="http://rep.evenex.dk/schema/evenex/eBusiness_01 http://rep.evenex.dk/schema/evenex/eBusiness_01/e-commerce_101.xsd"
并添加以下名称
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
SystemVersion="7.00.3.71.03"
答案 0 :(得分:2)
如果要删除命名空间,实际上要做的是创建具有相同本地名称但不在任何命名空间中的新元素。这个模板会这样做:
<xsl:template match="*" priority="-0.4">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
优先级是优先级高于身份模板(优先级为-0.5)。
要xsi:schemaLocation
,这是命名空间中的属性,您可以只使用一个简单的模板来忽略它
<xsl:template match="@xsi:schemaLocation" />
要为xsd添加不必要的名称空间声明,您需要匹配根元素Document
并将其添加到那里。您还可以同时添加SystemVersion
属性。
<xsl:template match="/e:Document">
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" SystemVersion="7.00.3.71.03">
<xsl:apply-templates select="@*|node()"/>
</Document>
</xsl:template>
注意这里使用名称空间前缀,以确保它匹配XML中指定的名称空间中的Document
元素(前缀e
将绑定到XSLT中的同一名称空间)。
试试这个XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:e = "http://rep.evenex.dk/schema/evenex/eBusiness_01"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="e xsi">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<!--Identity template, provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" priority="-0.4">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="/e:Document">
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" SystemVersion="7.00.3.71.03">
<xsl:apply-templates select="@*|node()"/>
</Document>
</xsl:template>
<xsl:template match="@xsi:schemaLocation" />
</xsl:stylesheet>