xslt - How to remove namespaces from XML and add new namespaces? -
i receiving xml file proper data. receiver of xml not want few xml namespaces not know why.
and few new namespaces should added.
i have input file as
<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>
i need remove few namespaces , add new namespaces. output should
<?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>
i trying xslt script remove first , add. it's not working
<?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 copies content output --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
i want remove following namespaces
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"
and add following namesapces
xmlns:xsd="http://www.w3.org/2001/xmlschema" systemversion="7.00.3.71.03"
if want remove namespace, doing creating new elements same local names, not in namespace. template this:
<xsl:template match="*" priority="-0.4"> <xsl:element name="{local-name()}"> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template>
the priority give higher priority identity template (which has priority -0.5).
to xsi:schemalocation
, attribute in namespace, can have simple template ignore it
<xsl:template match="@xsi:schemalocation" />
and add unnecessary namespace declaration xsd, need match root element document
, add there. can add systemversion
attribute @ same time.
<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>
note use of namespace prefix here, ensure matches document
element in namespace specified in xml (the prefix e
bound same namespace in xslt).
try 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 copies content 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>
Comments
Post a Comment