如何删除xmlns =""在一个元素?

时间:2017-10-26 17:00:22

标签: xml xslt xslt-1.0 element

问题在于我不知道如何从元素中删除xmlns。所以我的问题是:
如何在元素中删除标签NetLineAmount中的xmlns=""(参见"结果XML")?

源XML:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Factuur_insbou003.xsd">
<InvoiceLine>
    <LineNumber>1</LineNumber>
    <DeliveredQuantity>4</DeliveredQuantity>
    <DeliveredQuantityMeasureUnitCode>PCE</DeliveredQuantityMeasureUnitCode>
    <NumberOfInvoicingUnits>16.8</NumberOfInvoicingUnits>
    <NetLineAmount>44.76</NetLineAmount>
    <TradeItemIdentification>
        <GTIN>08711438159445</GTIN>
        <AdditionalItemIdentification>
            <TradeItemDescription>Vuren balk 60x150mm lengte 4200mm FSC70</TradeItemDescription>
            <Colour>Vuren</Colour>
            <PhysicalDimensions>
                <MeasurementUnitCode>CMT</MeasurementUnitCode>
            </PhysicalDimensions>
        </AdditionalItemIdentification>
    </TradeItemIdentification>
    <TradeItemCertificate>
        <CertificateClaimDescription>FSC Mix</CertificateClaimDescription>
    </TradeItemCertificate>
    <PriceInformation>
        <Price>2.96</Price>
        <PriceBase>
            <NumberOfUnitsInPriceBasis>1</NumberOfUnitsInPriceBasis>
            <MeasureUnitPriceBasis>MTR</MeasureUnitPriceBasis>
        </PriceBase>
    </PriceInformation>
</InvoiceLine>
</Invoice>

样式表XML:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"      
     xmlns="http://www.gs1.nl/factuur/insbou/004" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:output encoding="UTF-8" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/Invoice">
    <Invoice xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.gs1.nl/factuur/insbou/004 Factuur_insbou004.xsd">
        <xsl:apply-templates/>
    </Invoice>
</xsl:template>

<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@* | node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="DeliveredQuantityMeasureUnitCode">
<DeliveredQuantityUoM>
  <xsl:apply-templates select="@*|node()"/>
</DeliveredQuantityUoM>
</xsl:template>

<xsl:template match="NetLineAmount"> 
    <xsl:copy-of select="."/>
  <VariableOrderConditions>
    <VariableCode>LEN</VariableCode>
    <NumericValue>2000</NumericValue>
    <NumericValueUoM>MMT</NumericValueUoM>
  </VariableOrderConditions>
  <Operations>
    <OperationCode>CLE</OperationCode>
    <AlphanumericValue>Poetsen</AlphanumericValue>
    <AdditionalInformation>met chroompoets</AdditionalInformation>
    <OperationPrice>25</OperationPrice>
  </Operations>
</xsl:template>  

</xsl:transform>

结果XML(标记NetLineAmount中结果的问题为xmlns=""):

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.gs1.nl/factuur/insbou/004" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gs1.nl/factuur/insbou/004 Factuur_insbou004.xsd">
<InvoiceLine>
    <LineNumber>1</LineNumber>
    <DeliveredQuantity>4</DeliveredQuantity>
    <DeliveredQuantityUoM>PCE</DeliveredQuantityUoM>
    <NumberOfInvoicingUnits>16.8</NumberOfInvoicingUnits>
    <NetLineAmount xmlns="">44.76</NetLineAmount>
    <VariableOrderConditions>
        <VariableCode>LEN</VariableCode>
        <NumericValue>2000</NumericValue>
        <NumericValueUoM>MMT</NumericValueUoM>
    </VariableOrderConditions>
    <Operations>
        <OperationCode>CLE</OperationCode>
        <AlphanumericValue>Poetsen</AlphanumericValue>
        <AdditionalInformation>met chroompoets</AdditionalInformation>
        <OperationPrice>25</OperationPrice>
    </Operations>
    <TradeItemIdentification>
        <GTIN>08711438159445</GTIN>
        <AdditionalItemIdentification>
            <TradeItemDescription>Vuren balk 60x150mm lengte 4200mm FSC70</TradeItemDescription>
            <Colour>Vuren</Colour>
            <PhysicalDimensions>
                <MeasurementUnitCode>CMT</MeasurementUnitCode>
            </PhysicalDimensions>
        </AdditionalItemIdentification>
    </TradeItemIdentification>
    <TradeItemCertificate>
        <CertificateClaimDescription>FSC Mix</CertificateClaimDescription>
    </TradeItemCertificate>
    <PriceInformation>
        <Price>2.96</Price>
        <PriceBase>
            <NumberOfUnitsInPriceBasis>1</NumberOfUnitsInPriceBasis>
            <MeasureUnitPriceBasis>MTR</MeasureUnitPriceBasis>
        </PriceBase>
    </PriceInformation>
</InvoiceLine>
</Invoice>

通缉结果XML(标签NetLineAmount中没有xmlns=""):

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.gs1.nl/factuur/insbou/004" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gs1.nl/factuur/insbou/004 Factuur_insbou004.xsd">
<InvoiceLine>
    <LineNumber>1</LineNumber>
    <DeliveredQuantity>4</DeliveredQuantity>
    <DeliveredQuantityUoM>PCE</DeliveredQuantityUoM>
    <NumberOfInvoicingUnits>16.8</NumberOfInvoicingUnits>
    <NetLineAmount>44.76</NetLineAmount>
    <VariableOrderConditions>
        <VariableCode>LEN</VariableCode>
        <NumericValue>2000</NumericValue>
        <NumericValueUoM>MMT</NumericValueUoM>
    </VariableOrderConditions>
    <Operations>
        <OperationCode>CLE</OperationCode>
        <AlphanumericValue>Poetsen</AlphanumericValue>
        <AdditionalInformation>met chroompoets</AdditionalInformation>
        <OperationPrice>25</OperationPrice>
    </Operations>
    <TradeItemIdentification>
        <GTIN>08711438159445</GTIN>
        <AdditionalItemIdentification>
            <TradeItemDescription>Vuren balk 60x150mm lengte 4200mm FSC70</TradeItemDescription>
            <Colour>Vuren</Colour>
            <PhysicalDimensions>
                <MeasurementUnitCode>CMT</MeasurementUnitCode>
            </PhysicalDimensions>
        </AdditionalItemIdentification>
    </TradeItemIdentification>
    <TradeItemCertificate>
        <CertificateClaimDescription>FSC Mix</CertificateClaimDescription>
    </TradeItemCertificate>
    <PriceInformation>
        <Price>2.96</Price>
        <PriceBase>
            <NumberOfUnitsInPriceBasis>1</NumberOfUnitsInPriceBasis>
            <MeasureUnitPriceBasis>MTR</MeasureUnitPriceBasis>
        </PriceBase>
    </PriceInformation>
</InvoiceLine>
</Invoice>

1 个答案:

答案 0 :(得分:3)

XSLT的输出全部在默认命名空间http://www.gs1.nl/factuur/insbou/004

输入不在命名空间中。

在匹配<xsl:copy-of select="."/>的模板中执行NetLineAmount时,copy-of也会复制命名空间。这导致xmlns=""包含在输出中。

您可以从xmlns="http://www.gs1.nl/factuur/insbou/004"删除xsl:transform(这将导致输出不在命名空间中),或者您可以将副本替换为文字NetLineAmount元素。

  <xsl:template match="NetLineAmount"> 
    <NetLineAmount>
      <xsl:apply-templates select="@*|node()"/>
    </NetLineAmount>
    <VariableOrderConditions>
      <VariableCode>LEN</VariableCode>
      <NumericValue>2000</NumericValue>
      <NumericValueUoM>MMT</NumericValueUoM>
    </VariableOrderConditions>
    <Operations>
      <OperationCode>CLE</OperationCode>
      <AlphanumericValue>Poetsen</AlphanumericValue>
      <AdditionalInformation>met chroompoets</AdditionalInformation>
      <OperationPrice>25</OperationPrice>
    </Operations>
  </xsl:template>