Copying a complete XML Element using XSLT -
i translating 1 xml file another. want copy single, complete element original xml new xml. have tried using identity transformation can work if copy entire document. if try limit single element (which contains lot of data), values of in document - not xml tags , not in desired element. i'm relatively new xslt i'm not understanding identity transformation process correctly.
here sample input:
<?xml version="1.0" encoding="utf-8"?> <dataextract version="1.0"> <importexport>export</importexport> <entities> <entity> <data>unwanteddataa</data> <data>unwanteddatab</data> </entity> <entity> <data>unwanteddatac</data> <data>unwanteddatad</data> </entity> <entity> <entitydata> <data>abc</data> <data>def</data> <data>ghi</data> <specialdata id="1">definitely wanted</specialdata> </entitydata> </entity> </entities> </dataextract>
i want entitydata element , put out in input.
if use following copy entire input document, full duplicate xml tags, etc.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
but if try entitydata element using following code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="entitydata"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
i values instead of xml , entire document, not entitydata:
<?xml version="1.0" encoding="utf-8"?> export unwanteddataa unwanteddatab unwanteddatac unwanteddatad <entitydata> abc def ghi wanted </entitydata>
i want:
<?xml version="1.0" encoding="utf-8"?> <entitydata> <data>abc</data> <data>def</data> <data>ghi</data> <specialdata id="1">definitely wanted</specialdata> </entitydata>
i've tried different options copy , copy-of , have read talks copying entire document , possibly excluding few things. want exclude except entitydata element.
i want exclude except entitydata element.
that can achieved by:
xslt 1.0
<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="/dataextract"> <xsl:copy-of select="entities/entity/entitydata"/> </xsl:template> </xsl:stylesheet>
however, assumes there's 1 entitydata
element in entire input document. otherwise end ill-formed xml result (not having single root element).
Comments
Post a Comment