on 11-23-2015 2:51 PM
Hi Guys,
I have a requirement in which I need to add xsi:schemaLocation along with the attributes and also need to remove namespace.
Input
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Records NumberOfRecords="1" ChangeTimestamp="2015-11-23T12:15:51.0Z" xmlns:ns1="http://www.xyz.com/storeweaver/records/2.0.0">
<ns1:Record Type="add">
<ns1:ID>123456</ns1:OperatorID>
<ns1:FirstName>Test</ns1:FirstName>
<ns1:LastName>Test</ns1:LastName>
<ns1:Language>EN</ns1:Language>
<ns1:Address>abc</ns1:address>
</ns1:Record>
</ns1:Records>
Required Output
<?xml version="1.0" encoding="UTF-8"?>
<Records NumberOfRecords="1" ChangeTimestamp="2015-11-23T12:15:51.0Z" xsi:schemaLocation = "http://www.xyz.com/records/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.xyz.com/storeweaver/records/2.0.0">
<Record Type="add">
<ID>123456</OperatorID>
<FirstName>Test</FirstName>
<LastName>Test</LastName>
<Language>EN</Language>
<Address>abc</address>
</Record>
</Records>
XSLT
I have manage to write a small xslt which is adding the xsi:schemaLocation but I am not able to get the attributes and also not able to remove the namespace. I can use the Anonymizer bean bean to remove the namespace but I want to handle everything in xslt rather than half in xslt and half in the bean.
<?xml version="1.0" encoding="UTF-8"?>
<ns0:stylesheet version="1.0" xmlns:ns0="http://www.w3.org/1999/XSL/Transform">
<ns0:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<ns0:template match="/">
ns1:Records xsi:schemaLocation="http://xyz.com/records/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
NumberofRecords="1" ChangeTimestamp="2015-11-23T12:15:51.0Z" xmlns:ns1="http://www.xyz.com/storeweaver/records/2.0.0">
<ns0:copy-of select="/ns1:Records/Record"/>
</ns1:Records>
</ns0:template>
</ns0:stylesheet>
Note: I have just hard code the NumberofRecords and ChangeTimestamp as I didn't figure out how to pass them across through XSLT
Can somebody please let me know what else to add in the XSLT to achieve the above requirement.
Many Thanks,
Asif
Hi Asif,
With this source (your original XML has an error in the ns1:ID tag:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Records NumberOfRecords="1" ChangeTimestamp="2015-11-23T12:15:51.0Z" xmlns:ns1="http://www.xyz.com/storeweaver/records/2.0.0">
<ns1:Record Type="add">
<ns1:ID>123456</ns1:ID>
<ns1:FirstName>Test</ns1:FirstName>
<ns1:LastName>Test</ns1:LastName>
<ns1:Language>EN</ns1:Language>
<ns1:Address>abc</ns1:Address>
</ns1:Record>
</ns1:Records>
With this XSL:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:stylesheet version="1.0" xmlns:ns0="http://www.w3.org/1999/XSL/Transform">
<ns0:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<ns0:template match="/">
<ns1:Records xsi:schemaLocation="http://xyz.com/records/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" NumberofRecords="1" ChangeTimestamp="2015-11-23T12:15:51.0Z" xmlns:ns1="http://www.xyz.com/storeweaver/records/2.0.0">
<ns0:copy-of select="//ns1:Record"/>
</ns1:Records>
</ns0:template>
</ns0:stylesheet>
I get this XML with the attributes:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Records xmlns:ns1="http://www.xyz.com/storeweaver/records/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xyz.com/records/2.0.0" NumberofRecords="1" ChangeTimestamp="2015-11-23T12:15:51.0Z">
<ns1:Record Type="add">
<ns1:ID>123456</ns1:ID>
<ns1:FirstName>Test</ns1:FirstName>
<ns1:LastName>Test</ns1:LastName>
<ns1:Language>EN</ns1:Language>
<ns1:Address>abc</ns1:Address>
</ns1:Record>
</ns1:Records>
do you want to exclude the ns1 prefixes except in ns1:Records?
Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Asif,
Can the attribute value change?, If this doesn't go to change, you can use this XSL:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:stylesheet version="1.0" xmlns:ns0="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.xyz.com/storeweaver/records/2.0.0">
<ns0:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<ns0:template match="/">
<ns1:Records xsi:schemaLocation="http://xyz.com/records/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" NumberofRecords="1" ChangeTimestamp="2015-11-23T12:15:51.0Z" xmlns:ns1="http://www.xyz.com/storeweaver/records/2.0.0">
<Record Type="add">
<ns0:apply-templates select="//ns1:Record/child::*"/>
</Record>
</ns1:Records>
</ns0:template>
<ns0:template match="*">
<ns0:element name="{local-name(.)}">
<ns0:apply-templates/>
</ns0:element>
</ns0:template>
<ns0:template match="@*">
<ns0:copy/>
</ns0:template>
</ns0:stylesheet>
Regards.
Yes the attributes values will change as it will contain number of records and datetimestamp. I am able to solve the attrributes issue but not able to remove the ns1 from the xml.
I have tried your code but instead of remove ns1 from the xml it is adding it to all the fields.
I think we need to use replace function within the xslt to get rid of ns1 from the xml.
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.