| Subject: Re: [xsl] is there a way to output XML with invalid qnames? From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Fri, 06 Nov 2009 21:07:57 -0500 | 
I'm trying to format XML files so that I can display them as examples in a wiki. I'd like the element names in the examples to link to other wiki pages, so I'm trying to figure out the best way to take source like this:
<ns:elementName1> <ns:elementName2>value</ns:elementName2> </ns:elementName1>
And get output like this:
<[ns:elementName1|page 1]> <[ns:elementName2|page 2]>value</ns:elementName2> </ns:elementName1>
Of course, "[ns:elementName|page]" isn't a valid qname so just trying to rename the elements in an XML to XML transform doesn't work.
...
Before I give myself a headache trying to figure it out, I was wondering if anyone could suggest a better approach I should look into.
T:\ftemp>type jessica.xml <ns:elementName1 xmlns:ns="urn:X-Jessica"> <ns:elementName2>value</ns:elementName2> </ns:elementName1>
T:\ftemp>call xslt jessica.xml xml2wiki.xsl
<[ns:elementName1|page 1] xmlns:ns="urn:X-Jessica">
    <[ns:elementName2|page 2]>value</ns:elementName2>
</ns:elementName1>
T:\ftemp>type xml2wiki.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0"><xsl:template match="*">
  <!--reconstitute the element start tag-->
  <xsl:text><[</xsl:text>
  <xsl:value-of select="name(.)"/>|page <xsl:text/>
  <!--find the first in document order-->
  <xsl:variable name="first"
                select="key('names',name(.))[1]"/>
  <!--determine its ordinal based on the uniqueness of element names-->
  <xsl:value-of select="count(
                        ($first/preceding::* | $first/ancestor::*)
                        [generate-id(.)=
                         generate-id(key('names',name(.))[1])]) + 1"/>
  <xsl:text>]</xsl:text>
  <!--reconstitute the namespaces-->
  <xsl:for-each select="namespace::*[not(.=../../namespace::*) and
                                     name(.)!='xml']">
    <xsl:value-of select="concat(' ','xmlns')"/>
    <xsl:if test="name(.)">:<xsl:value-of select="name(.)"/></xsl:if>
    <xsl:value-of select="concat('="',.,'"')"/>
  </xsl:for-each>
  <!--reconstitute the attributes-->
  <xsl:for-each select="@*">
    <xsl:value-of select="concat(' ',name(.),'="')"/>
    <xsl:call-template name="do-text"/>
    <xsl:text>"</xsl:text>
  </xsl:for-each>
  <xsl:choose>
    <xsl:when test="node()">
      <!--reconstitute the content-->
      <xsl:text>></xsl:text>
      <xsl:apply-templates select="node()"/>
      <xsl:text></</xsl:text>
      <xsl:value-of select="name(.)"/>
      <xsl:text>></xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <!--empty element-->
      <xsl:text>/></xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template><xsl:template match="comment()"> <xsl:text><!--</xsl:text> <xsl:value-of select="."/> <xsl:text>--></xsl:text> </xsl:template>
<xsl:template match="processing-instruction()"> <xsl:text><?</xsl:text> <xsl:value-of select="concat(name(.),' ',.)"/> <xsl:text>?></xsl:text> </xsl:template>
<xsl:template match="text()" name="do-text">
  <xsl:param name="rest" select="."/>
  <xsl:choose>
    <xsl:when test="contains($rest,'<')">
      <xsl:value-of select="substring-before($rest,'<')"/>
      <xsl:text>&lt;</xsl:text>
      <xsl:call-template name="do-text">
        <xsl:with-param name="rest" select="substring-after($rest,'<')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$rest"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template><[xsl:template|page 4] match="comment()"> <[xsl:text|page 5]><!--</xsl:text> <[xsl:value-of|page 6] select="."/> <[xsl:text|page 5]>--></xsl:text> </xsl:template>
<[xsl:template|page 4] match="processing-instruction()"> <[xsl:text|page 5]><?</xsl:text> <[xsl:value-of|page 6] select="concat(name(.),' ',.)"/> <[xsl:text|page 5]>?></xsl:text> </xsl:template>
</xsl:stylesheet> T:\ftemp>rem
-- Upcoming: hands-on XSLT, XQuery and XSL-FO Washington DC Nov 2009 Interested in other classes? http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
| Current Thread | 
|---|
| 
 | 
| <- Previous | Index | Next -> | 
|---|---|---|
| [xsl] is there a way to output XML , Jessica Britton | Thread | RE: [xsl] is there a way to output , Michael Kay | 
| [xsl] is there a way to output XML , Jessica Britton | Date | [xsl] fo:basic-link, Mark Wilson | 
| Month |