Retrieve verbatim XML content

28 views
Skip to first unread message

Thomas Fazekas

unread,
May 4, 2014, 7:46:03 AM5/4/14
to xs...@googlegroups.com
Hi there,

I'm not sure whether this is strictly an XSLT related question, it mught be an XPath or XQuerry one. The fact of the matter is that I couldn't find/come up with a workable answer using any of these technologies.

Here goes : I want to retrieve verbatim the xml content of some selected XML tags.

For example, if I have the following XML code :
<a><b><c prop1="val1" prop2="val2"><e/><c><d prop1="val_a" prop2="val_b"><f/></d></b></a>

I need something that using an xpath expression like /a/b/* would give me :
<c prop1="val1" prop2="val2">
<d prop1="val_a" prop2="val_b">

In other words I'm interested in the verbatim copy of the XML code of all the tags of the current level but not underneath.
Can this be achieved with XSLT/XPath/XQuery ?

George Cristian Bina

unread,
May 4, 2014, 8:03:30 AM5/4/14
to thomas....@gmail.com, xs...@googlegroups.com
Dear Thomas,

Yes, a stylesheet like below will do exactly that:

<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

<xsl:template match="/">
<xsl:variable name="content">
<xsl:apply-templates select="/a/b/*" mode="shallowCopy"/>
</xsl:variable>
<result>
<xsl:copy-of select="$content"/>
</result>
</xsl:template>

<xsl:template match="*" mode="shallowCopy">
<xsl:copy>
<xsl:apply-templates select="@*" mode="shallowCopy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*" mode="shallowCopy">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>

Best Regards,
George
--
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
> --
> You received this message because you are subscribed to the Google
> Groups "XSLT" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to xslt+uns...@googlegroups.com
> <mailto:xslt+uns...@googlegroups.com>.
> To post to this group, send email to xs...@googlegroups.com
> <mailto:xs...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/xslt/fec70b48-eaea-4aaf-a392-4156857283e9%40googlegroups.com
> <https://groups.google.com/d/msgid/xslt/fec70b48-eaea-4aaf-a392-4156857283e9%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

George Cristian Bina

unread,
May 4, 2014, 8:13:34 AM5/4/14
to thomas....@gmail.com, xs...@googlegroups.com
... and you can simplify that down to

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<result>
<xsl:apply-templates select="/a/b/*" mode="shallowCopy"/>
</result>
</xsl:template>

<xsl:template match="*|@*" mode="shallowCopy">
<xsl:copy>
<xsl:apply-templates select="@*" mode="shallowCopy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


Best Regards,
George
--
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Thomas Fazekas

unread,
May 4, 2014, 1:25:14 PM5/4/14
to geo...@oxygenxml.com, xs...@googlegroups.com
Works like a charm ! Thanks !


Reply all
Reply to author
Forward
0 new messages