如何删除转换中的空xmlns=”“
?
下面是结果XML中显示的示例。注意空的xmlns
。
我正在与VisualStudio2019 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
子元素绑定到sales order名称空间,但仍然没有名称空间前缀,然后将默认名称空间(无名称空间前缀)声明为销售订单名称空间,并保留信封
和销售订单
元素名称空间的明确声明:
<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>
这是我的xml示例: 这是我的模板: 问题是,当我通过调用template创建输出文件时,输出元素
输入: 如何删除列顺序中没有值的记录? 期望输出: 我当前的XSL只删除空节点 我似乎无法掌握模板匹配来删除整个“记录”标签。。。非常感谢你!
我是XSLT的新手,命名空间有问题。这是我必须转换的XML: 我正在使用以下XSLT: 结果是: 我试图删除结果输入顺序中的名称空间,但它不适合我。有人能帮我学习XSLT吗?谢谢
问题内容: 我正在尝试使用sed删除空行: 但我没有运气。 例如,我有以下几行: 我希望它像: 这应该是什么代码? 问题答案: 您的“空”行中可能有空格或制表符。使用POSIX类与去除只含有空格的所有行: 使用ERE的较短版本,例如gnu sed: (请注意,sed的确实 不 支持PCRE)。
这个的代码应该是什么?
问题内容: 我想使用xsl文件转换一些xml并以某种方式输出结果(我使用的是Android Api Level 8)。 我当前的活动看起来像这样,但是转换器保持为空。LogCat引发一个with ,表示xml格式不正确,但是我确定它是正确的。 我在LogCat中发现了一条提示,提示在上述错误消息之前。 我究竟做错了什么? 这是要转换的xml文件(source.xml) 这是对应的xsl(produ