Reputation: 273
I have the following XML File and want to extract some portion of this XML File. I tried different xslts but I did not work. I couldn't understand where I made a mistake.
Sample File
<GetDataResponse xmlns="http://test">
<GetDataResult>
<Classification>TEST</Classification>
<data>
<ItemList xmlns="">
<Item>
<CommonMetadata>
<InfoCoreId>6719d364-7145-4f60-b5c2-07c38a29cce5</InfoCoreId>
<FirstReceivedTimestamp>2020-03-31T12:29:51.977Z</FirstReceivedTimestamp>
<LastUpdatedTimestamp>2020-03-31T12:29:51.977Z</LastUpdatedTimestamp>
<CacheExpiresOnTimestamp>2020-04-28T12:29:51.977Z</CacheExpiresOnTimestamp>
<IsDeleted>False</IsDeleted>
<ADSName>Test</ADSName>
<ADSURI/>
<ADSInfoCoreId>885bc303-13b2-48e2-8186-8d60cb0ceecf</ADSInfoCoreId>
<IngestionName>Test</IngestionName>
<IngestionId>bd0c753e-5262-48cd-b946-7f3a1220ac31</IngestionId>
<Description>Test</Description>
<SourceAssignedId/>
<SourceChecksum/>
<Author/>
<Geography>
<KMLRepresentation>
<kml:kml xmlns:kml="http://www.opengis.net/kml/2.2">
<kml:Placemark>
<kml:description>TEST</kml:description>
<kml:Point>
<kml:coordinates>111111,22222</kml:coordinates>
</kml:Point>
</kml:Placemark>
</kml:kml>
</KMLRepresentation>
</Geography>
<BSO/>
<ModeOfOperation>Live</ModeOfOperation>
</CommonMetadata>
</Item>
</ItemList>
</data>
<xsltOutputFormat/>
</GetDataResult>
</GetDataResponse>
I want to get the all the nodes and their values under the "data" element node like below;
Expected File
<ItemList xmlns="">
<Item>
<CommonMetadata>
<InfoCoreId>6719d364-7145-4f60-b5c2-07c38a29cce5</InfoCoreId>
<FirstReceivedTimestamp>2020-03-31T12:29:51.977Z</FirstReceivedTimestamp>
<LastUpdatedTimestamp>2020-03-31T12:29:51.977Z</LastUpdatedTimestamp>
<CacheExpiresOnTimestamp>2020-04-28T12:29:51.977Z</CacheExpiresOnTimestamp>
<IsDeleted>False</IsDeleted>
<ADSName>Test</ADSName>
<ADSURI/>
<ADSInfoCoreId>885bc303-13b2-48e2-8186-8d60cb0ceecf</ADSInfoCoreId>
<IngestionName>Test</IngestionName>
<IngestionId>bd0c753e-5262-48cd-b946-7f3a1220ac31</IngestionId>
<Description>Test</Description>
<SourceAssignedId/>
<SourceChecksum/>
<Author/>
<Geography>
<KMLRepresentation>
<kml:kml xmlns:kml="http://www.opengis.net/kml/2.2">
<kml:Placemark>
<kml:description>TEST</kml:description>
<kml:Point>
<kml:coordinates>111111,22222</kml:coordinates>
</kml:Point>
</kml:Placemark>
</kml:kml>
</KMLRepresentation>
</Geography>
<BSO/>
<ModeOfOperation>Live</ModeOfOperation>
</CommonMetadata>
</Item>
</ItemList>
Applied XSLT-1
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="GetDataResponse/GetDataResult/data">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Applied XSLT-2
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="GetDataResponse/GetDataResult/data">
<xsl:copy-of select="node()"/>
</xsl:template>
</xsl:stylesheet>
But none of these work for me. What is the problem with my xslt?
Thank you for your help.
Upvotes: 0
Views: 341
Reputation: 116959
Or simply:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="//ItemList"/>
</xsl:template>
</xsl:stylesheet>
Although personally I would prefer a more explicit path:
<xsl:copy-of select="*/*/*/ItemList"/>
Upvotes: 1
Reputation: 5905
Solution without namespaces :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="//ItemList"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jxDiMBU
Upvotes: 1
Reputation: 2714
I think you should review the namespaces used in your input file.
Here's a solution that does what you want. Note that you can't ignore the namespaces, you have to take it into account when precessing the nodes.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tst="http://test">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="tst:GetDataResponse/tst:GetDataResult/tst:data"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/gVhDDyS
Upvotes: 2