how to add namespace and new attribute to root node - xslt

I am using xslt to add namespace and new attribute to root node.I need to make this xml file
<?xml version="1.0" encoding="utf-8"?>
<Document Hub="" DocType="Invoice" ID="" BatchNumber="" DocumentCreatedDate="2016-08-09">
<HeaderInformation></HeaderInformation>
<Summary></Summary>
</Document>
To be Like
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="http://Anylink" Hub="ADP" DocumentCreatedDate="2016-08-09" PartnershipId="123">
<HeaderInformation></HeaderInformation>
<Summary></Summary>
</Document>

Related

How to transform an XML document using XSLT

I need to take an XML file and create a new output file.
During creation of the output file, I need to total the quantities by material
This is my input file: -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<MATERIAL>Material1</MATERIAL>
<SALES_DIST>FS</SALES_DIST>
<REGION>North</REGION>
<FIELDNM001>EA</FIELDNM001>
<MONTH>2020-01-01</MONTH>
<FIELDNM002>1</FIELDNM002>
</record>
<record>
<MATERIAL>Material1</MATERIAL>
<SALES_DIST>FS</SALES_DIST>
<REGION>North</REGION>
<FIELDNM001>EA</FIELDNM001>
<MONTH>2020-01-01</MONTH>
<FIELDNM002>1</FIELDNM002>
</record>
</data-set>
What I need to end up with is a file like this: -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<MATERIAL>Material1</MATERIAL>
<SALES_DIST>FS</SALES_DIST>
<REGION>North</REGION>
<FIELDNM001>EA</FIELDNM001>
<MONTH>2020-01-01</MONTH>
<FIELDNM002>2</FIELDNM002>
</record>
</data-set>
Any ideas on what the xslt document should look like to achieve this?
Many thanks in advance.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<MATERIAL>Material1</MATERIAL>
<SALES_DIST>FS</SALES_DIST>
<REGION>North</REGION>
<FIELDNM001>EA</FIELDNM001>
<MONTH>2020-01-01</MONTH>
<FIELDNM002>2</FIELDNM002>
</record>
</data-set>
Starting from this reply, the following works for me using xsltproc on Linux:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gt="http://www.gtech.com/lsp/2009-09-23"
exclude-result-prefixes="gt">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<MATERIAL><xsl:value-of select="//MATERIAL" /></MATERIAL>
<SALES_DIST><xsl:value-of select="//SALES_DIST" /></SALES_DIST>
<REGION><xsl:value-of select="//REGION" /></REGION>
<FIELDNM001><xsl:value-of select="//FIELDNM001" /></FIELDNM001>
<MONTH><xsl:value-of select="//MONTH" /></MONTH>
<FIELDNM002><xsl:value-of select="sum(//FIELDNM002[.])"/></FIELDNM002>
</record>
</data-set>
</xsl:template>
</xsl:stylesheet>
If other fields of the records in xml are not equals, it takes only the first records' values

How to use XSLT when my XML contains a Prefix?

I have this XML below and I'm trying to print the content of the MyField element using XSLT. Unfortunately I can't make it work. Can you help me fix my XSLT?
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="my_xsl.xsl"?>
<ns:Collection>
<Load>
<Item>
<MyField>Please Print Me</MyField>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="ns:Collection/Load/Item/MyField" />
Your XML is not namespace well formed because the namespace prefix "ns" has not been bound to any namespace URI.
XSLT cannot process XML input unless it is namespace well formed.
Use namespace for ns: in the root element

Extract XML Multiple Namespace with XSL

I have this XML with namespace and i need to extract on segment "NewDataSet"
I have a xsl code but it's not works
<?xml version="1.0" encoding="UTF-8"?>
<Listado_OrdenesResponse xmlns='http://tempuri.org/' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<Listado_OrdenesResult>
<diffgr:diffgram xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>
<NewDataSet xmlns=''>
<RowNum diffgr:id='RowNum1' msdata:rowOrder='0'>
<MATNR>10000101</MATNR> <AUFNR>731200000047</AUFNR>
<MENGE>385</MENGE>
<MEINS>G</MEINS>
</RowNum>
<RowNum diffgr:id='RowNum2' msdata:rowOrder='1'>
<MATNR>45000528</MATNR>
<AUFNR>731200000047</AUFNR>
<MENGE>540</MENGE>
<MEINS>KG</MEINS>
</RowNum>
</NewDataSet>
</diffgr:diffgram>
</Listado_OrdenesResult>
</Listado_OrdenesResponse>
I need to extract like this segment , NewDataSet.
<NewDataSet> <RowNum>
<MATNR>10000101</MATNR>
<AUFNR>731200000047</AUFNR>
</RowNum>
<RowNum>
<MATNR>45000528</MATNR>
<AUFNR>731200000047</AUFNR>
</RowNum>
</NewDataSet>
<!-- Need To Extract -->
I have this code but the return is not as expected.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="//NewDataSet">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
When I run the above XSL, I got this XML.
I will have the segment without namespace
<?xml version="1.0" encoding="UTF-8"?>
<RowNum xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
diffgr:id="RowNum1"
msdata:rowOrder="0">
<MATNR>10000101</MATNR>
<AUFNR>731200000047</AUFNR>
<MENGE>385</MENGE>
<MEINS>G</MEINS>
</RowNum>
<RowNum xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
diffgr:id="RowNum2"
msdata:rowOrder="1">
<MATNR>45000528</MATNR>
<AUFNR>731200000047</AUFNR>
<MENGE>540</MENGE>
<MEINS>KG</MEINS>
</RowNum>
Can you Help me?
In XSLT 2.0, use <xsl:copy-of select="XXXX" copy-namespaces='no'/>.
In XSLT 1.0 you need to use a variant of the identity template, copying elements using <xsl:element name="{local-name()}" namespace="namespace-uri()"/>.
Please don't ask XSLT questions without saying which version you are using, as many things are easier with XSLT 2.0 or 3.0!

Check for XML header

How can i check in XSLT that an XML contains XML headers?
Example XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Data>
<test>1</test>
</Data>
I want to check if the <?xml/> header exists.
<xsl:if test="xml header exists">
do something
</xsl:if>
// otherwise?
Thanks
The <?xml version="1.0" encoding="UTF-8" standalone="yes"?> is called the XML declaration and it is not part of the XSLT/XPath/XQuery data model so you can't access it or check for it using XSLT/XPath/XQuery.

xml namespaces in xsl transform - ignore blank?

I'm new to xsl and am trying to write a template to transform xml to html.
I have an xml document that begins
<?xml version="1.0" encoding="UTF-8"?>
<data xmlns:autn="http://schemas.com/aci/"
xmlns="http://iptc.org/std/nar/2006-10-01/">
<name>Bob</name>
and my xsl template begins
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:autn="http://schemas.autonomy.com/aci/">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
...
<body>
<p>user name:</p>
<p><xsl:value-of select="data/name"/></p>
The problem is, if I do
I don't get anything back for the value-of select.
If I do
I get 'Bob' but I lose all my html.
What am I missing?
You are missing the default namespace of the XML document:
xmlns="http://iptc.org/std/nar/2006-10-01/"
Add it to the XSLT as well:
<xsl:stylesheet version="1.0"
xmlns:mynamespace="http://iptc.org/std/nar/2006-10-01/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:autn="http://schemas.autonomy.com/aci/">
And use that namespace in the xsl:value-of:
<xsl:value-of select="mynamespace:data/mynamespace:name" />