[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

this script converts an instance path to an RNG path

Subject: this script converts an instance path to an RNG path
From: S Woodside <sbwoodside@xxxxxxxxx>
Date: Mon, 31 Mar 2003 18:02:51 -0500
instance path
Another XSLT code snippet for anyone using RNG out there. Here's a snippet to convert an instance document path into an XPath-like string that will locate the correct segment of a Relax NG (RNG) grammar.

The input param "fragment" might be something like "/resume/header/contact and then output will be something like "//*[@name='resume']//*[@name='header']//*[@name='contact']/*". You would then call dyn:evaluate() in EXSLT (or whatever equivalent you have) on the resulting string to turn it into a genuine XPath.

Comments welcome as always.

<!-- Converts an instance path to an RNG path -->
<xsl:template name="inst2rngPath">
<xsl:param name="fragment"/>
<xsl:choose>
<xsl:when test="starts-with($fragment, '/')"> <!-- strip beginning / -->
<xsl:call-template name="inst2rngPath">
<xsl:with-param name="fragment" select="substring-after($fragment, '/')"/>
</xsl:call-template>
</xsl:when><xsl:otherwise>
<xsl:text>//*[@name='</xsl:text>
<xsl:choose><xsl:when test="substring-before($fragment, '/')">
<xsl:value-of select="substring-before($fragment, '/')"/>
</xsl:when><xsl:otherwise>
<xsl:value-of select="$fragment"/>
</xsl:otherwise></xsl:choose>
<xsl:text>']</xsl:text>
<xsl:choose>
<xsl:when test="contains($fragment, '/')"> <!-- recursive step -->
<xsl:call-template name="inst2rngPath">
<xsl:with-param name="fragment" select="substring-after($fragment, '/')"/>
</xsl:call-template>
</xsl:when><xsl:otherwise> <!-- default step -->
<xsl:text>/*</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


simon
--
www.simonwoodside.com -- 99% Devil, 1% Angel


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.