使用模板进行 XSLT 转换:删除空的 xmlns=""
XSLT transform with templates: remove empty xmlns=""
如何删除转换中的空 xmlns=""
?
下面是结果 XML 中显示的示例。注意空 xmlns
.
我正在使用 VisualStudio 2019 16.8.4。
这是我需要转换的来源XML。
<?xml version="1.0" encoding="UTF-8"?>
<wPOREQ payloadID='113084912-000001' timestamp='2020-12-16T14:05:36-06:00' version='3.1'>
<Header mode='Production'>
<From>
<Credential domain='DUNS'>
<Identity>060480999</Identity>
</Credential>
</From>
<To>
<Credential domain='DUNS'>
<Identity>060480999</Identity>
</Credential>
</To>
<Sender>
<Credential domain='DUNS'>
<Identity>196488888</Identity>
<SharedSecret>111</SharedSecret>
</Credential>
</Sender>
</Header>
<Order deploymentMode='production' rush='N'>
<PONumber>59test</PONumber>
<ReferenceNumber>59240249</ReferenceNumber>
<WCSSCustomerId>0810999</WCSSCustomerId>
<OrderExtrinsic>
<Extrinsic name='PONUM'>MM000327999</Extrinsic>
</OrderExtrinsic>
<ShipTo>
<WCSSShipTo>0001</WCSSShipTo>
<LocationCode/>
<Name1>SHIP TO THIS</Name1>
<Name2/>
<Address1>99 MEDICAL CENTER DRIVE</Address1>
<Address2>HSC BMRC</Address2>
<Address3>BK TEST</Address3>
<City>ANYTOWN</City>
<State>WV</State>
<ZipCode>265053409</ZipCode>
<CountryCode>US</CountryCode>
<Phone>
<AreaCode/>
<PhNumber/>
<Ext/>
</Phone>
<Fax>
<AreaCode/>
<PhNumber/>
<Ext/>
</Fax>
<Attention>RM:8707</Attention>
<Email/>
</ShipTo>
<OrderDate>2020-12-16T14:05:36-06:00</OrderDate>
<DeliverByDate>2021-01-31</DeliverByDate>
<RequestedShipDate/>
<RequestedCarrier>UPSS</RequestedCarrier>
<ShippingMessage>CUST CARRIER ACCT: 021FW0</ShippingMessage>
<LineItems>
<LineItem>
<LineNumber>1</LineNumber>
<SKU>10037475</SKU>
<SKUDescription>WVU LETTERHEAD</SKUDescription>
<Quantity>1000</Quantity>
<OrderUOM>SH</OrderUOM>
<UnitPrice currency='USD'>125.50</UnitPrice>
<NameFromBC/>
<Phone1FromBC/>
<EmailFromBC>katharine.belcher</EmailFromBC>
<DeliverByDate/>
<RequestedShipDate>2021-01-31</RequestedShipDate>
<LineMessage>PRINT 1 COPY(S) OF ALL PDF FIL</LineMessage>
<LineMessage>ES STARTING WITH: 59240249 WVU</LineMessage>
<SupplierAuxPartId/>
<LineItemExtrinsic>
<Extrinsic name='PriceUOM'>M</Extrinsic>
</LineItemExtrinsic>
</LineItem>
</LineItems>
</Order>
</wPOREQ>
这是我为 XSLT 生成的内容:
<?xml version="1.0" encoding="utf-8"?>
<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="/">
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>
<!--<xsl:value-of select="Header/MessageId"/>-->
</MessageId>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<xsl:apply-templates select="wPOREQ/Order"/>
<xsl:apply-templates select="wPOREQ/Order/ShipTo"/>
<xsl:apply-templates select="wPOREQ/Order/LineItems/LineItem"/>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
</xsl:template>
<xsl:template match="wPOREQ/Order">
<CustAccount>
<xsl:value-of select="WCSSCustomerId"/>
</CustAccount>
<CustomerRef>
<xsl:value-of select="ReferenceNumber"/>
</CustomerRef>
<InventLocationId>
<xsl:value-of select="WCSSCustomerId"/>
</InventLocationId>
<InventSiteId>
<xsl:value-of select="WCSSCustomerId"/>
</InventSiteId>
<InvoiceAccount>
<xsl:value-of select="WCSSCustomerId"/>
</InvoiceAccount>
<ProductType>
<xsl:value-of select="WCSSCustomerId"/>
</ProductType>
<PurchOrderFormNum>
<xsl:value-of select="PONumber"/>
</PurchOrderFormNum>
<ReceiptDateRequested>
<xsl:value-of select="DeliverByDate"/>
</ReceiptDateRequested>
<SalesGroup>
<xsl:value-of select="WCSSCustomerId"/>
</SalesGroup>
<SalesPoolId>
<xsl:value-of select="10" />
</SalesPoolId>
<ShipCarrierDeliveryContact>
<xsl:value-of select="ShipTo/Attention"/>
</ShipCarrierDeliveryContact>
</xsl:template>
<xsl:template match="ShipTo">
<TableDlvAddr class="entity">
<City>
<xsl:value-of select="City"/>
</City>
<CountryRegionId>
<xsl:choose>
<xsl:when test="CountryCode = 'US'">USA</xsl:when>
<xsl:otherwise>
<xsl:value-of select="USA" />
</xsl:otherwise>
</xsl:choose>
</CountryRegionId>
<LocationName>
<xsl:value-of select="Name1"/>
</LocationName>
<State>
<xsl:value-of select="State"/>
</State>
<Street>
<xsl:value-of select="concat(Address1,'
', Address2,'
', Address3)"/>
</Street>
<ZipCode>
<xsl:value-of select="substring(ZipCode,1,5)"/>
</ZipCode>
</TableDlvAddr>
</xsl:template>
<xsl:template match="LineItem">
<SalesLine class="entity">
<CustomerRef>
<xsl:value-of select="../../ReferenceNumber"/>
</CustomerRef>
<ItemId>
<xsl:value-of select="SKU"/>
</ItemId>
<LineNum>
<xsl:value-of select="LineNumber"/>
</LineNum>
<PurchorderFormNum>
<xsl:value-of select="../../PONumber"/>
</PurchorderFormNum>
<SalesPrice>
<xsl:value-of select="UnitPrice"/>
</SalesPrice>
<SalesQty>
<xsl:value-of select="Quantity"/>
</SalesQty>
<SalesUnit>
<xsl:value-of select="OrderUOM"/>
</SalesUnit>
</SalesLine>
</xsl:template>
</xsl:stylesheet>
然而,输出产生:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId />
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount xmlns="">0810147</CustAccount>
<CustomerRef xmlns="">BK012521-01</CustomerRef>
<InventLocationId xmlns="">0810147</InventLocationId>
<InventSiteId xmlns="">0810147</InventSiteId>
<InvoiceAccount xmlns="">0810147</InvoiceAccount>
<ProductType xmlns="">0810147</ProductType>
<PurchOrderFormNum xmlns="">595280620001b3</PurchOrderFormNum>
<ReceiptDateRequested xmlns="">2021-01-31</ReceiptDateRequested>
<SalesGroup xmlns="">0810147</SalesGroup>
<SalesPoolId xmlns="">10</SalesPoolId>
<ShipCarrierDeliveryContact xmlns="">RM:8707,SALLY WEAVER</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity" xmlns="">
<City>MORGANTOWN</City>
<CountryRegionId>USA</CountryRegionId>
<LocationName>WEST VIRGINIA UNIVERSITY</LocationName>
<State>WV</State>
<Street>
64 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST
</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity" xmlns="">
<CustomerRef>BK012521-01</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchorderFormNum>595280620001b3</PurchorderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
期望的输出是这样的:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId />
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>0810147</CustAccount>
<CustomerRef>59240249</CustomerRef>
<InventLocationId>0810147</InventLocationId>
<InventSiteId>0810147</InventSiteId>
<InvoiceAccount>0810147</InvoiceAccount>
<ProductType>0810147</ProductType>
<PurchOrderFormNum>595280620001</PurchOrderFormNum>
<ReceiptDateRequested>2021-01-31</ReceiptDateRequested>
<SalesGroup>0810147</SalesGroup>
<SalesPoolId>10</SalesPoolId>
<ShipCarrierDeliveryContact>RM:8707</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity">
<City>MORGANTOWN</City>
<CountryRegionId>USA </CountryRegionId>
<LocationName>WEST VIRGINIA UNIVERSITY</LocationName>
<State>WV</State>
<Street>64 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity">
<CustomerRef>59240249</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchOrderFormNum>595280620001</PurchOrderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
在您的样式表中使用名称空间前缀声明那些名称空间,并在您确实希望绑定到名称空间的元素上使用这些名称空间前缀。
那么没有绑定到命名空间的元素将在没有前缀的情况下被序列化,并且不必重新声明命名空间为空:
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:so="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<m:Envelope>
<m:Header>
<m:MessageId>
<!--<xsl:value-of select="Header/MessageId"/>-->
</m:MessageId>
<m:Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</m:Action>
</m:Header>
<m:Body>
<m:MessageParts>
<so:SalesOrder>
<so:SalesTable class="entity">
<xsl:apply-templates select="wPOREQ/Order"/>
<xsl:apply-templates select="wPOREQ/Order/ShipTo"/>
<xsl:apply-templates select="wPOREQ/Order/LineItems/LineItem"/>
</so:SalesTable>
</so:SalesOrder>
</m:MessageParts>
</m:Body>
</m:Envelope>
</xsl:template>
<xsl:template match="wPOREQ/Order">
<CustAccount>
<xsl:value-of select="WCSSCustomerId"/>
</CustAccount>
<CustomerRef>
<xsl:value-of select="ReferenceNumber"/>
</CustomerRef>
<InventLocationId>
<xsl:value-of select="WCSSCustomerId"/>
</InventLocationId>
<InventSiteId>
<xsl:value-of select="WCSSCustomerId"/>
</InventSiteId>
<InvoiceAccount>
<xsl:value-of select="WCSSCustomerId"/>
</InvoiceAccount>
<ProductType>
<xsl:value-of select="WCSSCustomerId"/>
</ProductType>
<PurchOrderFormNum>
<xsl:value-of select="PONumber"/>
</PurchOrderFormNum>
<ReceiptDateRequested>
<xsl:value-of select="DeliverByDate"/>
</ReceiptDateRequested>
<SalesGroup>
<xsl:value-of select="WCSSCustomerId"/>
</SalesGroup>
<SalesPoolId>
<xsl:value-of select="10" />
</SalesPoolId>
<ShipCarrierDeliveryContact>
<xsl:value-of select="ShipTo/Attention"/>
</ShipCarrierDeliveryContact>
</xsl:template>
<xsl:template match="ShipTo">
<TableDlvAddr class="entity">
<City>
<xsl:value-of select="City"/>
</City>
<CountryRegionId>
<xsl:choose>
<xsl:when test="CountryCode = 'US'">USA</xsl:when>
<xsl:otherwise>
<xsl:value-of select="USA" />
</xsl:otherwise>
</xsl:choose>
</CountryRegionId>
<LocationName>
<xsl:value-of select="Name1"/>
</LocationName>
<State>
<xsl:value-of select="State"/>
</State>
<Street>
<xsl:value-of select="concat(Address1,'
', Address2,'
', Address3)"/>
</Street>
<ZipCode>
<xsl:value-of select="substring(ZipCode,1,5)"/>
</ZipCode>
</TableDlvAddr>
</xsl:template>
<xsl:template match="LineItem">
<SalesLine class="entity">
<CustomerRef>
<xsl:value-of select="../../ReferenceNumber"/>
</CustomerRef>
<ItemId>
<xsl:value-of select="SKU"/>
</ItemId>
<LineNum>
<xsl:value-of select="LineNumber"/>
</LineNum>
<PurchorderFormNum>
<xsl:value-of select="../../PONumber"/>
</PurchorderFormNum>
<SalesPrice>
<xsl:value-of select="UnitPrice"/>
</SalesPrice>
<SalesQty>
<xsl:value-of select="Quantity"/>
</SalesQty>
<SalesUnit>
<xsl:value-of select="OrderUOM"/>
</SalesUnit>
</SalesLine>
</xsl:template>
</xsl:stylesheet>
产生这个输出:
<?xml version="1.0" encoding="utf-8"?>
<m:Envelope xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:so="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<m:Header>
<m:MessageId/>
<m:Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</m:Action>
</m:Header>
<m:Body>
<m:MessageParts>
<so:SalesOrder>
<so:SalesTable class="entity">
<CustAccount>0810999</CustAccount>
<CustomerRef>59240249</CustomerRef>
<InventLocationId>0810999</InventLocationId>
<InventSiteId>0810999</InventSiteId>
<InvoiceAccount>0810999</InvoiceAccount>
<ProductType>0810999</ProductType>
<PurchOrderFormNum>59test</PurchOrderFormNum>
<ReceiptDateRequested>2021-01-31</ReceiptDateRequested>
<SalesGroup>0810999</SalesGroup>
<SalesPoolId>10</SalesPoolId>
<ShipCarrierDeliveryContact>RM:8707</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity">
<City>ANYTOWN</City>
<CountryRegionId>USA</CountryRegionId>
<LocationName>SHIP TO THIS</LocationName>
<State>WV</State>
<Street>99 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity">
<CustomerRef>59240249</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchorderFormNum>59test</PurchorderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</so:SalesTable>
</so:SalesOrder>
</m:MessageParts>
</m:Body>
</m:Envelope>
如果您希望将在“无命名空间”中序列化的那些 SalesOrder
后代元素绑定到销售订单命名空间,但仍然没有命名空间前缀,则声明默认命名空间(无命名空间-prefix) 成为销售订单命名空间并保留 Envelope
和 SalesOrder
元素的命名空间的显式声明:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>
<!--<xsl:value-of select="Header/MessageId"/>-->
</MessageId>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<xsl:apply-templates select="wPOREQ/Order"/>
<xsl:apply-templates select="wPOREQ/Order/ShipTo"/>
<xsl:apply-templates select="wPOREQ/Order/LineItems/LineItem"/>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
</xsl:template>
<xsl:template match="wPOREQ/Order">
<CustAccount>
<xsl:value-of select="WCSSCustomerId"/>
</CustAccount>
<CustomerRef>
<xsl:value-of select="ReferenceNumber"/>
</CustomerRef>
<InventLocationId>
<xsl:value-of select="WCSSCustomerId"/>
</InventLocationId>
<InventSiteId>
<xsl:value-of select="WCSSCustomerId"/>
</InventSiteId>
<InvoiceAccount>
<xsl:value-of select="WCSSCustomerId"/>
</InvoiceAccount>
<ProductType>
<xsl:value-of select="WCSSCustomerId"/>
</ProductType>
<PurchOrderFormNum>
<xsl:value-of select="PONumber"/>
</PurchOrderFormNum>
<ReceiptDateRequested>
<xsl:value-of select="DeliverByDate"/>
</ReceiptDateRequested>
<SalesGroup>
<xsl:value-of select="WCSSCustomerId"/>
</SalesGroup>
<SalesPoolId>
<xsl:value-of select="10" />
</SalesPoolId>
<ShipCarrierDeliveryContact>
<xsl:value-of select="ShipTo/Attention"/>
</ShipCarrierDeliveryContact>
</xsl:template>
<xsl:template match="ShipTo">
<TableDlvAddr class="entity">
<City>
<xsl:value-of select="City"/>
</City>
<CountryRegionId>
<xsl:choose>
<xsl:when test="CountryCode = 'US'">USA</xsl:when>
<xsl:otherwise>
<xsl:value-of select="USA" />
</xsl:otherwise>
</xsl:choose>
</CountryRegionId>
<LocationName>
<xsl:value-of select="Name1"/>
</LocationName>
<State>
<xsl:value-of select="State"/>
</State>
<Street>
<xsl:value-of select="concat(Address1,'
', Address2,'
', Address3)"/>
</Street>
<ZipCode>
<xsl:value-of select="substring(ZipCode,1,5)"/>
</ZipCode>
</TableDlvAddr>
</xsl:template>
<xsl:template match="LineItem">
<SalesLine class="entity">
<CustomerRef>
<xsl:value-of select="../../ReferenceNumber"/>
</CustomerRef>
<ItemId>
<xsl:value-of select="SKU"/>
</ItemId>
<LineNum>
<xsl:value-of select="LineNumber"/>
</LineNum>
<PurchorderFormNum>
<xsl:value-of select="../../PONumber"/>
</PurchorderFormNum>
<SalesPrice>
<xsl:value-of select="UnitPrice"/>
</SalesPrice>
<SalesQty>
<xsl:value-of select="Quantity"/>
</SalesQty>
<SalesUnit>
<xsl:value-of select="OrderUOM"/>
</SalesUnit>
</SalesLine>
</xsl:template>
</xsl:stylesheet>
产生此输出:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId/>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>0810999</CustAccount>
<CustomerRef>59240249</CustomerRef>
<InventLocationId>0810999</InventLocationId>
<InventSiteId>0810999</InventSiteId>
<InvoiceAccount>0810999</InvoiceAccount>
<ProductType>0810999</ProductType>
<PurchOrderFormNum>59test</PurchOrderFormNum>
<ReceiptDateRequested>2021-01-31</ReceiptDateRequested>
<SalesGroup>0810999</SalesGroup>
<SalesPoolId>10</SalesPoolId>
<ShipCarrierDeliveryContact>RM:8707</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity">
<City>ANYTOWN</City>
<CountryRegionId>USA</CountryRegionId>
<LocationName>SHIP TO THIS</LocationName>
<State>WV</State>
<Street>99 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity">
<CustomerRef>59240249</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchorderFormNum>59test</PurchorderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
如何删除转换中的空 xmlns=""
?
下面是结果 XML 中显示的示例。注意空 xmlns
.
我正在使用 VisualStudio 2019 16.8.4。
这是我需要转换的来源XML。
<?xml version="1.0" encoding="UTF-8"?>
<wPOREQ payloadID='113084912-000001' timestamp='2020-12-16T14:05:36-06:00' version='3.1'>
<Header mode='Production'>
<From>
<Credential domain='DUNS'>
<Identity>060480999</Identity>
</Credential>
</From>
<To>
<Credential domain='DUNS'>
<Identity>060480999</Identity>
</Credential>
</To>
<Sender>
<Credential domain='DUNS'>
<Identity>196488888</Identity>
<SharedSecret>111</SharedSecret>
</Credential>
</Sender>
</Header>
<Order deploymentMode='production' rush='N'>
<PONumber>59test</PONumber>
<ReferenceNumber>59240249</ReferenceNumber>
<WCSSCustomerId>0810999</WCSSCustomerId>
<OrderExtrinsic>
<Extrinsic name='PONUM'>MM000327999</Extrinsic>
</OrderExtrinsic>
<ShipTo>
<WCSSShipTo>0001</WCSSShipTo>
<LocationCode/>
<Name1>SHIP TO THIS</Name1>
<Name2/>
<Address1>99 MEDICAL CENTER DRIVE</Address1>
<Address2>HSC BMRC</Address2>
<Address3>BK TEST</Address3>
<City>ANYTOWN</City>
<State>WV</State>
<ZipCode>265053409</ZipCode>
<CountryCode>US</CountryCode>
<Phone>
<AreaCode/>
<PhNumber/>
<Ext/>
</Phone>
<Fax>
<AreaCode/>
<PhNumber/>
<Ext/>
</Fax>
<Attention>RM:8707</Attention>
<Email/>
</ShipTo>
<OrderDate>2020-12-16T14:05:36-06:00</OrderDate>
<DeliverByDate>2021-01-31</DeliverByDate>
<RequestedShipDate/>
<RequestedCarrier>UPSS</RequestedCarrier>
<ShippingMessage>CUST CARRIER ACCT: 021FW0</ShippingMessage>
<LineItems>
<LineItem>
<LineNumber>1</LineNumber>
<SKU>10037475</SKU>
<SKUDescription>WVU LETTERHEAD</SKUDescription>
<Quantity>1000</Quantity>
<OrderUOM>SH</OrderUOM>
<UnitPrice currency='USD'>125.50</UnitPrice>
<NameFromBC/>
<Phone1FromBC/>
<EmailFromBC>katharine.belcher</EmailFromBC>
<DeliverByDate/>
<RequestedShipDate>2021-01-31</RequestedShipDate>
<LineMessage>PRINT 1 COPY(S) OF ALL PDF FIL</LineMessage>
<LineMessage>ES STARTING WITH: 59240249 WVU</LineMessage>
<SupplierAuxPartId/>
<LineItemExtrinsic>
<Extrinsic name='PriceUOM'>M</Extrinsic>
</LineItemExtrinsic>
</LineItem>
</LineItems>
</Order>
</wPOREQ>
这是我为 XSLT 生成的内容:
<?xml version="1.0" encoding="utf-8"?>
<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="/">
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>
<!--<xsl:value-of select="Header/MessageId"/>-->
</MessageId>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<xsl:apply-templates select="wPOREQ/Order"/>
<xsl:apply-templates select="wPOREQ/Order/ShipTo"/>
<xsl:apply-templates select="wPOREQ/Order/LineItems/LineItem"/>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
</xsl:template>
<xsl:template match="wPOREQ/Order">
<CustAccount>
<xsl:value-of select="WCSSCustomerId"/>
</CustAccount>
<CustomerRef>
<xsl:value-of select="ReferenceNumber"/>
</CustomerRef>
<InventLocationId>
<xsl:value-of select="WCSSCustomerId"/>
</InventLocationId>
<InventSiteId>
<xsl:value-of select="WCSSCustomerId"/>
</InventSiteId>
<InvoiceAccount>
<xsl:value-of select="WCSSCustomerId"/>
</InvoiceAccount>
<ProductType>
<xsl:value-of select="WCSSCustomerId"/>
</ProductType>
<PurchOrderFormNum>
<xsl:value-of select="PONumber"/>
</PurchOrderFormNum>
<ReceiptDateRequested>
<xsl:value-of select="DeliverByDate"/>
</ReceiptDateRequested>
<SalesGroup>
<xsl:value-of select="WCSSCustomerId"/>
</SalesGroup>
<SalesPoolId>
<xsl:value-of select="10" />
</SalesPoolId>
<ShipCarrierDeliveryContact>
<xsl:value-of select="ShipTo/Attention"/>
</ShipCarrierDeliveryContact>
</xsl:template>
<xsl:template match="ShipTo">
<TableDlvAddr class="entity">
<City>
<xsl:value-of select="City"/>
</City>
<CountryRegionId>
<xsl:choose>
<xsl:when test="CountryCode = 'US'">USA</xsl:when>
<xsl:otherwise>
<xsl:value-of select="USA" />
</xsl:otherwise>
</xsl:choose>
</CountryRegionId>
<LocationName>
<xsl:value-of select="Name1"/>
</LocationName>
<State>
<xsl:value-of select="State"/>
</State>
<Street>
<xsl:value-of select="concat(Address1,'
', Address2,'
', Address3)"/>
</Street>
<ZipCode>
<xsl:value-of select="substring(ZipCode,1,5)"/>
</ZipCode>
</TableDlvAddr>
</xsl:template>
<xsl:template match="LineItem">
<SalesLine class="entity">
<CustomerRef>
<xsl:value-of select="../../ReferenceNumber"/>
</CustomerRef>
<ItemId>
<xsl:value-of select="SKU"/>
</ItemId>
<LineNum>
<xsl:value-of select="LineNumber"/>
</LineNum>
<PurchorderFormNum>
<xsl:value-of select="../../PONumber"/>
</PurchorderFormNum>
<SalesPrice>
<xsl:value-of select="UnitPrice"/>
</SalesPrice>
<SalesQty>
<xsl:value-of select="Quantity"/>
</SalesQty>
<SalesUnit>
<xsl:value-of select="OrderUOM"/>
</SalesUnit>
</SalesLine>
</xsl:template>
</xsl:stylesheet>
然而,输出产生:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId />
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount xmlns="">0810147</CustAccount>
<CustomerRef xmlns="">BK012521-01</CustomerRef>
<InventLocationId xmlns="">0810147</InventLocationId>
<InventSiteId xmlns="">0810147</InventSiteId>
<InvoiceAccount xmlns="">0810147</InvoiceAccount>
<ProductType xmlns="">0810147</ProductType>
<PurchOrderFormNum xmlns="">595280620001b3</PurchOrderFormNum>
<ReceiptDateRequested xmlns="">2021-01-31</ReceiptDateRequested>
<SalesGroup xmlns="">0810147</SalesGroup>
<SalesPoolId xmlns="">10</SalesPoolId>
<ShipCarrierDeliveryContact xmlns="">RM:8707,SALLY WEAVER</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity" xmlns="">
<City>MORGANTOWN</City>
<CountryRegionId>USA</CountryRegionId>
<LocationName>WEST VIRGINIA UNIVERSITY</LocationName>
<State>WV</State>
<Street>
64 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST
</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity" xmlns="">
<CustomerRef>BK012521-01</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchorderFormNum>595280620001b3</PurchorderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
期望的输出是这样的:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId />
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>0810147</CustAccount>
<CustomerRef>59240249</CustomerRef>
<InventLocationId>0810147</InventLocationId>
<InventSiteId>0810147</InventSiteId>
<InvoiceAccount>0810147</InvoiceAccount>
<ProductType>0810147</ProductType>
<PurchOrderFormNum>595280620001</PurchOrderFormNum>
<ReceiptDateRequested>2021-01-31</ReceiptDateRequested>
<SalesGroup>0810147</SalesGroup>
<SalesPoolId>10</SalesPoolId>
<ShipCarrierDeliveryContact>RM:8707</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity">
<City>MORGANTOWN</City>
<CountryRegionId>USA </CountryRegionId>
<LocationName>WEST VIRGINIA UNIVERSITY</LocationName>
<State>WV</State>
<Street>64 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity">
<CustomerRef>59240249</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchOrderFormNum>595280620001</PurchOrderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
在您的样式表中使用名称空间前缀声明那些名称空间,并在您确实希望绑定到名称空间的元素上使用这些名称空间前缀。
那么没有绑定到命名空间的元素将在没有前缀的情况下被序列化,并且不必重新声明命名空间为空:
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:so="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<m:Envelope>
<m:Header>
<m:MessageId>
<!--<xsl:value-of select="Header/MessageId"/>-->
</m:MessageId>
<m:Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</m:Action>
</m:Header>
<m:Body>
<m:MessageParts>
<so:SalesOrder>
<so:SalesTable class="entity">
<xsl:apply-templates select="wPOREQ/Order"/>
<xsl:apply-templates select="wPOREQ/Order/ShipTo"/>
<xsl:apply-templates select="wPOREQ/Order/LineItems/LineItem"/>
</so:SalesTable>
</so:SalesOrder>
</m:MessageParts>
</m:Body>
</m:Envelope>
</xsl:template>
<xsl:template match="wPOREQ/Order">
<CustAccount>
<xsl:value-of select="WCSSCustomerId"/>
</CustAccount>
<CustomerRef>
<xsl:value-of select="ReferenceNumber"/>
</CustomerRef>
<InventLocationId>
<xsl:value-of select="WCSSCustomerId"/>
</InventLocationId>
<InventSiteId>
<xsl:value-of select="WCSSCustomerId"/>
</InventSiteId>
<InvoiceAccount>
<xsl:value-of select="WCSSCustomerId"/>
</InvoiceAccount>
<ProductType>
<xsl:value-of select="WCSSCustomerId"/>
</ProductType>
<PurchOrderFormNum>
<xsl:value-of select="PONumber"/>
</PurchOrderFormNum>
<ReceiptDateRequested>
<xsl:value-of select="DeliverByDate"/>
</ReceiptDateRequested>
<SalesGroup>
<xsl:value-of select="WCSSCustomerId"/>
</SalesGroup>
<SalesPoolId>
<xsl:value-of select="10" />
</SalesPoolId>
<ShipCarrierDeliveryContact>
<xsl:value-of select="ShipTo/Attention"/>
</ShipCarrierDeliveryContact>
</xsl:template>
<xsl:template match="ShipTo">
<TableDlvAddr class="entity">
<City>
<xsl:value-of select="City"/>
</City>
<CountryRegionId>
<xsl:choose>
<xsl:when test="CountryCode = 'US'">USA</xsl:when>
<xsl:otherwise>
<xsl:value-of select="USA" />
</xsl:otherwise>
</xsl:choose>
</CountryRegionId>
<LocationName>
<xsl:value-of select="Name1"/>
</LocationName>
<State>
<xsl:value-of select="State"/>
</State>
<Street>
<xsl:value-of select="concat(Address1,'
', Address2,'
', Address3)"/>
</Street>
<ZipCode>
<xsl:value-of select="substring(ZipCode,1,5)"/>
</ZipCode>
</TableDlvAddr>
</xsl:template>
<xsl:template match="LineItem">
<SalesLine class="entity">
<CustomerRef>
<xsl:value-of select="../../ReferenceNumber"/>
</CustomerRef>
<ItemId>
<xsl:value-of select="SKU"/>
</ItemId>
<LineNum>
<xsl:value-of select="LineNumber"/>
</LineNum>
<PurchorderFormNum>
<xsl:value-of select="../../PONumber"/>
</PurchorderFormNum>
<SalesPrice>
<xsl:value-of select="UnitPrice"/>
</SalesPrice>
<SalesQty>
<xsl:value-of select="Quantity"/>
</SalesQty>
<SalesUnit>
<xsl:value-of select="OrderUOM"/>
</SalesUnit>
</SalesLine>
</xsl:template>
</xsl:stylesheet>
产生这个输出:
<?xml version="1.0" encoding="utf-8"?>
<m:Envelope xmlns:m="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:so="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<m:Header>
<m:MessageId/>
<m:Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</m:Action>
</m:Header>
<m:Body>
<m:MessageParts>
<so:SalesOrder>
<so:SalesTable class="entity">
<CustAccount>0810999</CustAccount>
<CustomerRef>59240249</CustomerRef>
<InventLocationId>0810999</InventLocationId>
<InventSiteId>0810999</InventSiteId>
<InvoiceAccount>0810999</InvoiceAccount>
<ProductType>0810999</ProductType>
<PurchOrderFormNum>59test</PurchOrderFormNum>
<ReceiptDateRequested>2021-01-31</ReceiptDateRequested>
<SalesGroup>0810999</SalesGroup>
<SalesPoolId>10</SalesPoolId>
<ShipCarrierDeliveryContact>RM:8707</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity">
<City>ANYTOWN</City>
<CountryRegionId>USA</CountryRegionId>
<LocationName>SHIP TO THIS</LocationName>
<State>WV</State>
<Street>99 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity">
<CustomerRef>59240249</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchorderFormNum>59test</PurchorderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</so:SalesTable>
</so:SalesOrder>
</m:MessageParts>
</m:Body>
</m:Envelope>
如果您希望将在“无命名空间”中序列化的那些 SalesOrder
后代元素绑定到销售订单命名空间,但仍然没有命名空间前缀,则声明默认命名空间(无命名空间-prefix) 成为销售订单命名空间并保留 Envelope
和 SalesOrder
元素的命名空间的显式声明:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>
<!--<xsl:value-of select="Header/MessageId"/>-->
</MessageId>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<xsl:apply-templates select="wPOREQ/Order"/>
<xsl:apply-templates select="wPOREQ/Order/ShipTo"/>
<xsl:apply-templates select="wPOREQ/Order/LineItems/LineItem"/>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>
</xsl:template>
<xsl:template match="wPOREQ/Order">
<CustAccount>
<xsl:value-of select="WCSSCustomerId"/>
</CustAccount>
<CustomerRef>
<xsl:value-of select="ReferenceNumber"/>
</CustomerRef>
<InventLocationId>
<xsl:value-of select="WCSSCustomerId"/>
</InventLocationId>
<InventSiteId>
<xsl:value-of select="WCSSCustomerId"/>
</InventSiteId>
<InvoiceAccount>
<xsl:value-of select="WCSSCustomerId"/>
</InvoiceAccount>
<ProductType>
<xsl:value-of select="WCSSCustomerId"/>
</ProductType>
<PurchOrderFormNum>
<xsl:value-of select="PONumber"/>
</PurchOrderFormNum>
<ReceiptDateRequested>
<xsl:value-of select="DeliverByDate"/>
</ReceiptDateRequested>
<SalesGroup>
<xsl:value-of select="WCSSCustomerId"/>
</SalesGroup>
<SalesPoolId>
<xsl:value-of select="10" />
</SalesPoolId>
<ShipCarrierDeliveryContact>
<xsl:value-of select="ShipTo/Attention"/>
</ShipCarrierDeliveryContact>
</xsl:template>
<xsl:template match="ShipTo">
<TableDlvAddr class="entity">
<City>
<xsl:value-of select="City"/>
</City>
<CountryRegionId>
<xsl:choose>
<xsl:when test="CountryCode = 'US'">USA</xsl:when>
<xsl:otherwise>
<xsl:value-of select="USA" />
</xsl:otherwise>
</xsl:choose>
</CountryRegionId>
<LocationName>
<xsl:value-of select="Name1"/>
</LocationName>
<State>
<xsl:value-of select="State"/>
</State>
<Street>
<xsl:value-of select="concat(Address1,'
', Address2,'
', Address3)"/>
</Street>
<ZipCode>
<xsl:value-of select="substring(ZipCode,1,5)"/>
</ZipCode>
</TableDlvAddr>
</xsl:template>
<xsl:template match="LineItem">
<SalesLine class="entity">
<CustomerRef>
<xsl:value-of select="../../ReferenceNumber"/>
</CustomerRef>
<ItemId>
<xsl:value-of select="SKU"/>
</ItemId>
<LineNum>
<xsl:value-of select="LineNumber"/>
</LineNum>
<PurchorderFormNum>
<xsl:value-of select="../../PONumber"/>
</PurchorderFormNum>
<SalesPrice>
<xsl:value-of select="UnitPrice"/>
</SalesPrice>
<SalesQty>
<xsl:value-of select="Quantity"/>
</SalesQty>
<SalesUnit>
<xsl:value-of select="OrderUOM"/>
</SalesUnit>
</SalesLine>
</xsl:template>
</xsl:stylesheet>
产生此输出:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId/>
<Action>http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</Action>
</Header>
<Body>
<MessageParts>
<SalesOrder xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder">
<SalesTable class="entity">
<CustAccount>0810999</CustAccount>
<CustomerRef>59240249</CustomerRef>
<InventLocationId>0810999</InventLocationId>
<InventSiteId>0810999</InventSiteId>
<InvoiceAccount>0810999</InvoiceAccount>
<ProductType>0810999</ProductType>
<PurchOrderFormNum>59test</PurchOrderFormNum>
<ReceiptDateRequested>2021-01-31</ReceiptDateRequested>
<SalesGroup>0810999</SalesGroup>
<SalesPoolId>10</SalesPoolId>
<ShipCarrierDeliveryContact>RM:8707</ShipCarrierDeliveryContact>
<TableDlvAddr class="entity">
<City>ANYTOWN</City>
<CountryRegionId>USA</CountryRegionId>
<LocationName>SHIP TO THIS</LocationName>
<State>WV</State>
<Street>99 MEDICAL CENTER DRIVE
HSC BMRC
BK TEST</Street>
<ZipCode>26505</ZipCode>
</TableDlvAddr>
<SalesLine class="entity">
<CustomerRef>59240249</CustomerRef>
<ItemId>10037475</ItemId>
<LineNum>1</LineNum>
<PurchorderFormNum>59test</PurchorderFormNum>
<SalesPrice>125.50</SalesPrice>
<SalesQty>1000</SalesQty>
<SalesUnit>SH</SalesUnit>
</SalesLine>
</SalesTable>
</SalesOrder>
</MessageParts>
</Body>
</Envelope>