I have an XML and transforming it using XSLT. But the result transformed xml file containing unnecessary namespces.
UPDATED
How to make the xslt not to have the xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" in the result xml file.
Add the attribute "exclude-result-prefixes" on xsl:stylesheet node and list the namespaces you want to exclude
Sample
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="2.0" exclude-result-prefixes="xsl fo xs fn">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="#*|*|comment()">
<xsl:copy>
<xsl:apply-templates select="#*|*|text()|comment()|processing-instruction()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Yeah!
Jean-Michel is right, you must use exclude-result-prefixe="soapenv", thats works for me.
This question was aswered here:
How to remove namespace from the output xml?
Related
I read through forums and i am unable to understand why the output is empty. It might be a simple thing i am missing.
I tried debugging in VS 2017 and it does not give any output any help in this matter is appreciated. IF i input only the ns0:EFACT_D96A_ORDERS_EAN008 node as an input to XSLT the output comes with the "test" content
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 userCSharp" version="1.0" xmlns:ns1="http://Microsoft.LobServices.Sap/2007/03/Types/Idoc/3/ORDERS05//740" xmlns:ns0="http://Microsoft.LobServices.Sap/2007/03/Idoc/3/ORDERS05//740/Send" xmlns:ns2="http://Microsoft.LobServices.Sap/2007/03/Types/Idoc/Common/" xmlns:s0="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006" xmlns:ns3="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ins0="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/InterchangeXML">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<xsl:apply-templates select="/s0:EFACT_D96A_ORDERS_EAN008" />
</xsl:template>
<xsl:template match="/s0:EFACT_D96A_ORDERS_EAN008">
<ns0:Send>
<ns0:idocData>
Test
</ns0:idocData>
</ns0:Send>
</xsl:template>
</xsl:stylesheet>
Input file-
<ins0:EdifactInterchangeXml DelimiterSetSerializedData="39:-1:-1:43:58:63:-1:46:-1" xmlns:ins0="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/InterchangeXML">
<ns0:UNA xmlns:ns0="http://schemas.microsoft.com/Edi/EdifactServiceSchema">
<UNA1>58</UNA1>
</ns0:UNA>
<ns0:UNB xmlns:ns0="http://schemas.microsoft.com/Edi/EdifactServiceSchema">
<UNB1>
<UNB1.1>ABCD</UNB1.1>
<UNB1.2>5</UNB1.2>
</UNB1>
</ns0:UNB>
<TransactionSetGroup>
<TransactionSet DocType="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006#EFACT_D96A_ORDERS_EAN008">
<ns0:EFACT_D96A_ORDERS_EAN008 xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006">
</ns0:EFACT_D96A_ORDERS_EAN008>
</TransactionSet>
</TransactionSetGroup>
<ns0:UNZ xmlns:ns0="http://schemas.microsoft.com/Edi/EdifactServiceSchema">
<UNZ1>1</UNZ1>
<UNZ2>86</UNZ2>
</ns0:UNZ>
</ins0:EdifactInterchangeXml>
A forward slash / at the start of the expression matches the document node, so doing select="/s0:EFACT_D96A_ORDERS_EAN008" will only select s0:EFACT_D96A_ORDERS_EAN008 if it is a child of the document node. i.e. if it is the root element, which it isn't.
To select s0:EFACT_D96A_ORDERS_EAN008 regardless of where it is in the document do this...
<xsl:apply-templates select="//s0:EFACT_D96A_ORDERS_EAN008" />
You also need to remove the single forward slash from the match expression too (You don't need the double-slashes in the match expression, as the match will work regardless of where the element is in the document)
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 userCSharp" version="1.0" xmlns:ns1="http://Microsoft.LobServices.Sap/2007/03/Types/Idoc/3/ORDERS05//740" xmlns:ns0="http://Microsoft.LobServices.Sap/2007/03/Idoc/3/ORDERS05//740/Send" xmlns:ns2="http://Microsoft.LobServices.Sap/2007/03/Types/Idoc/Common/" xmlns:s0="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006" xmlns:ns3="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ins0="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/InterchangeXML">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<xsl:apply-templates select="//s0:EFACT_D96A_ORDERS_EAN008" />
</xsl:template>
<xsl:template match="s0:EFACT_D96A_ORDERS_EAN008">
<ns0:Send>
<ns0:idocData>
Test
</ns0:idocData>
</ns0:Send>
</xsl:template>
</xsl:stylesheet>
I have the following XML with a cdata tag that I would like to extract the text from?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cus:TestData xmlns:cus="http://test.namespace.com/data">
<![CDATA[testValue]]></cus:TestData >
How can I achieve this in XSLT?
I was briefly trying with the following
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/Name"/>"
</xsl:template>
</xsl:stylesheet>
But it doesn't seem to be working
Also the XML doesn't also have the same prefix or namespace, it changes
This is not really an issue with CData. Your XSLT is currently looking for an element called Name, under the root element, which does not exist in your XML. If your XML source is the one you are actually using, you can just do this...
<xsl:value-of select="/*"/>
But supposing your XML looked like this...
<cus:TestData xmlns:cus="http://test.namespace.com/data">
<cus:Name><![CDATA[testValue]]></cus:Name>
</cus:TestData>
Then, you would need to account for the namespace in your XSLT, as Name is in a namespace in your XML, but your XSLT is currently looking for a Name element in no namespace.
Something like this would do:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://test.namespace.com/data">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/c:Name"/>"
</xsl:template>
</xsl:stylesheet>
Note, the prefixes don't need to match, but the namespace URI does.
If the namespace URI could actually vary, you could do something like this instead...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/*[local-name() = 'Name']"/>"
</xsl:template>
</xsl:stylesheet>
This question already has answers here:
XSLT Transform doesn't work until I remove root node
(2 answers)
Closed 6 years ago.
The below xml is not transforming because of xmlns, I tried without xmlns, it working as expected. but i am receiving input with xmlns. Please suggest how can i overcome it.
Requirement: To retrieve productBenefitHeaders from the xml.
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<productData xmlns="http://www.example.org/consolidated">
<productResponse>
<product>
<status>
<isError>false</isError>
</status>
<productBenefit>
<productBenefitCategory>CARDs</productBenefitCategory>
<productBenefitId>12AA</productBenefitId>
<productBenefitHeader>Philips</productBenefitHeader>
</productBenefit>
<productBenefit>
<productBenefitCategory>CARDs</productBenefitCategory>
<productBenefitId>12AB</productBenefitId>
<productBenefitHeader>Samsung</productBenefitHeader>
</productBenefit>
</product>
</productResponse>
<productData>
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="html" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:template match="/">
<xsl:for-each select="productData/productResponse/product/productBenefit">
<xsl:value-of select="productBenefitHeader"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The elements in the source xml are in the namespace http://www.example.org/consolidated. While you are searching for elements without specifying the namespace.
To search with the namespace you need to add the namespace in the stylesheet tag and set a prefix for it, in this case I used 'pref'.
xmlns:pref="http://www.example.org/consolidated"
Now you can use the prefix in your xsl while specifying the elements you are looking for. This is your xsl but with the prefix added.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pref="http://www.example.org/consolidated">
<xsl:output method="html" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:template match="/">
<xsl:for-each select="pref:productData/pref:productResponse/pref:product/pref:productBenefit">
<xsl:value-of select="pref:productBenefitHeader"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Also, make sure your end tag is correct. Currently the last tag in your example xml is not a closing tag.
I want to remove an XML element from an XML file. The tag that I want to remove is named as xfdf:field.
How do I specify this in my xslt ? I tried this and I am getting an error saything "org.apache.xpath.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: xfdf ".
Here is my xslt.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="xfdf:field"></xsl:template>
</xsl:stylesheet>
Here is my xml.
<xfa:datasets
xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"
xmlns:dd="http://ns.adobe.com/data-description/" xmlns:tns="http://hostname"
xmlns:xfdf="http://ns.adobe.com/xfdf/">
<xfa:data>
<tns:form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:formHeader>
<tns:formId>formid</tns:formId>
<tns:revId>Rev123</tns:revId>
<tns:agencyId>agency</tns:agencyId>
<tns:progId>program</tns:progId>
<tns:serviceId>service</tns:serviceId>
</tns:formHeader>
<tns:formFields>
<tns:date>08-13-1967</tns:date>
<tns:agreementBetween></tns:agreementBetween>
<tns:ncr>xxxx</tns:ncr>
<tns:formConfirmationInfo>
<tns:confNbrLbl>nbrlabel</tns:confNbrLbl>
<tns:confNbrData>1231</tns:confNbrData>
<tns:custNameLbl>3332</tns:custNameLbl>
<tns:custNameData>dasdas</tns:custNameData>
<tns:dateLbl>date</tns:dateLbl>
<tns:dateData>01012001</tns:dateData>
</tns:formConfirmationInfo>
</tns:formFields>
<xfdf:field xmlns:xfdfi="http://ns.adobe.com/xfdf-transition/"
xfdfi:original="FSAPPLICATIONDATA_">
<!--irrelevant data omitted-->
</xfdf:field>
</tns:form>
</xfa:data>
</xfa:datasets>
You need to specify the namespace in your XSLT file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xfdf="http://ns.adobe.com/xfdf/">
I'm trying to transform one xml file to output another xml file, and I need the attribute "account" to be output identically to how it appears below. I have a bunch of these values in the file, most are not working.
For values of account like 0x0406 it is output as 0.0.06. But for values like 0x002d it leaves them alone and they come through the way I want.
Any ideas?
Initial XML:
<?xml version="1.0" encoding="UTF-8"?>
...
<foo account="0x0406" other-stuff="blah" something-name="blah again"/>
...
This is my xslt template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://some-internal-thing/user">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
...
<xsl:attribute name="account"><xsl:value-of select="#account"/></xsl:attribute>
...
The described problem cannot be reproduced.
I have run the following transformation with 10 different xslt processors (Msxml3, Msxml4, Msxml6, .NET XslTransform, .NET XslCompiledTransform, AltovaXml(for XSLT 1.01.0), Saxon6.5.4, Saxon 9.1.07, Saxon 9.1.07.NET and XML-SPY-XSLT2.0) and they all produce the same correct result.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://some-internal-thing/user">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:attribute name="account"><xsl:value-of select="#account"/></xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when this transformation is performed on the following XML document:
<?xml version="1.0" encoding="UTF-8"?>
<foo account="0x0406" other-stuff="blah" something-name="blah again"/>
the expected, correct result is produced:
<foo account="0x0406" />
In case your XSLT processor is not one of these and you really get a wrong result, this is a bug and should be reported to the vendors.
Can you try to change the encoding ? For example "ASCII" instead of "UTF-8".