Re: [xsl] Split into numbered files: without side-effect? (XSLT 2)

Subject: Re: [xsl] Split into numbered files: without side-effect? (XSLT 2)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 28 Sep 2007 12:22:36 -0400
At 11:31 AM 9/28/2007, I wrote:
You can also build keys that index to paths, parts of paths or quasi-paths, especially if you have functions (XSLT 2.0) that help make them for you.

Along these lines, here's a utility stylesheet with a function to build XPaths to nodes.


I'm sure it could be more elegant (m:step is crying out for templates :-), and await suggestions.

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:m="http://www.mulberrytech.com/xslt/util"; exclude-result-prefixes="m">

<!-- test with this:

  <xsl:template match="*">
    <xsl:text>&#xA;</xsl:text>
    <xsl:value-of select="m:path(.)"/>
    <xsl:apply-templates/>
  </xsl:template>

<xsl:template match="text()"/>

-->

<xsl:function name="m:path" as="xs:string?">
  <xsl:param name="who" as="node()?"/>
  <xsl:if test="exists($who)">
    <xsl:value-of select="concat(m:path($who/parent::*),m:step($who))"/>
  </xsl:if>
</xsl:function>

<xsl:function name="m:step" as="xs:string">
<xsl:param name="who" as="node()"/>
<xsl:value-of separator="''">
<xsl:text>/</xsl:text>
<xsl:choose>
<xsl:when test="$who/self::element()">
<xsl:value-of select="name($who)"/>
<xsl:if test="count($who/../element()[name() = name($who)]) gt 1">
<xsl:text>[</xsl:text>
<xsl:value-of
select="count($who/preceding-sibling::*[name() = name($who)]) + 1"/>
<xsl:text>]</xsl:text>
</xsl:if>
</xsl:when>
<xsl:when test="$who/self::attribute()">
<xsl:text>@</xsl:text>
<xsl:value-of select="name($who)"/>
</xsl:when>
<xsl:when test="$who/self::comment()">
<xsl:text>comment()</xsl:text>
<xsl:if test="count($who/../comment()) gt 1">
<xsl:text>[</xsl:text>
<xsl:value-of select="count($who/preceding-sibling::comment()) + 1"/>
<xsl:text>]</xsl:text>
</xsl:if>
</xsl:when>
<xsl:when test="$who/self::processing-instruction()">
<xsl:text>processing-instruction()</xsl:text>
<xsl:if test="count($who/../processing-instruction()) gt 1">
<xsl:text>[</xsl:text>
<xsl:value-of
select="count($who/preceding-sibling::processing-instruction()) + 1"/>
<xsl:text>]</xsl:text>
</xsl:if>
</xsl:when>
<xsl:when test="$who/self::text()">
<xsl:text>text()</xsl:text>
<xsl:if test="count($who/../text()) gt 1">
<xsl:text>[</xsl:text>
<xsl:value-of select="count($who/preceding-sibling::text()) + 1"/>
<xsl:text>]</xsl:text>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:value-of>
</xsl:function>


</xsl:stylesheet>




====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread