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

RE: Search and Replace in XSLT

Subject: RE: Search and Replace in XSLT
From: Jeni Tennison <jeni.tennison@xxxxxxxxxxxxxxxx>
Date: Fri, 11 Aug 2000 15:33:49 +0100
replace in xslt
Ciaran,

>1. Does this mean that the "tag" element becomes a child element of "img"
>
>img
>|
>|_ _ tag
>
>I want to include the tag element but not the img element in my output XML,
>so that when I get <img src="x.bmp" alt="[tag]"> then the output is just
><tag/> ( sorry, I didn't specify that before !!)

You don't have to output anything you don't want to: XSLT will only produce
the output that you tell it to produce.  If you have a template that
matches on 'img', then you can produce just the 'tag' element from it:

<xsl:template match="img">
  <xsl:element name="{translate(@alt, '[]', '')}" />
</xsl:template>

>2. How can I maipulate the case where I'd have mutiple attributes with
>values
>e.g.
><img src="x.bmp" alt="[tag1 value=tag1]Some text[tag2 value=tag2]"/>
>
>The reason I use '[' and ']' is that '<' and '>' can't reside inside the
>attribute.
>
>FYI - the reason I'm taking this approach is that in my MSHTML editor I
>can't handle
>unknown tags, so I was thinking of using a placeholder image with the "alt"
>value
>containing the XML markup that can't be recognised. Then when going back to
>XML,
>I recognise the image as a placeholder and extract from the "alt" attribute
>the
>XML markup and display as such. I'm open to any other ideas on this approach
>!!

Blimey, the lengths we'll go to to use a tool that we feel comfortable
with.  An alternative approach might be to use a tool that's designed to
write/read/edit XML to allow you to (even *help you to*) write the XML that
you want to write.  There's a list of free XML editors at
[http://www.garshol.priv.no/download/xmltools/cat_ix.html#SC_XMLEditors].
You could even write it by hand in any text editor: you're going to have to
be typing all that pseudo-XML by hand anyway.

Also, by the way, attributes *can* contain '<' (and '>'), it's just that
you have to escape '<' to &lt; to stop the XML parser getting confused and
thinking that you're trying to start a tag.  '>' can go in as it is.  So
you could alternatively use the pseudo-XML syntax:

<img src="x.bmp" alt="&lt;tag1 value=tag1>Some text&lt;tag2 value=tag2>"/>

I hope that clears things up a bit,

Jeni

P.S. I just couldn't resist: here's a shot at parsing the pseudo-XML
without any error handling if your pseudo-XML syntax is off, and that
hasn't included quotes around attribute values because you didn't in your
example:

<xsl:template name="parse-pseudo-XML-content">
  <xsl:param name="text" />
  <xsl:choose>
    <xsl:when test="contains($text, '[')">
      <xsl:value-of select="substring-before($text, '[')" />
      <xsl:call-template name="parse-pseudo-XML-element">
        <xsl:with-param name="text"
                        select="substring-after(substring-before($text, ']'), 
                                                '[')" />
      </xsl:call-template>
      <xsl:call-template name="parse-pseudo-XML-content">
        <xsl:with-param name="text"
                        select="substring-after($text, ']')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="parse-pseudo-XML-element">
  <xsl:param name="text" />
  <xsl:choose>
    <xsl:when test="contains($text, ' ')">
      <xsl:element name="{substring-before($text, ' ')}">
        <xsl:call-template name="parse-pseudo-XML-attributes">
          <xsl:with-param name="text"
                          select="substring-after($text, ' ')" />
        </xsl:call-template>
      </xsl:element>
    </xsl:when>
    <xsl:otherwise>
      <xsl:element name="{$text}" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="parse-pseudo-XML-attributes">
  <xsl:param name="text" />
  <xsl:choose>
    <xsl:when test="contains($text, ' ')">
      <xsl:call-template name="parse-pseudo-XML-attribute">
        <xsl:with-param name="text" select="substring-before($text, ' ')" />
      </xsl:call-template>
      <xsl:call-template name="parse-pseudo-XML-attributes">
        <xsl:with-param name="text" select="substring-after($text, ' ')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="parse-pseudo-XML-attribute">
        <xsl:with-param name="text" select="$text" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="parse-pseudo-XML-attribute">
  <xsl:param name="text" />
  <xsl:attribute name="{substring-before($text, '=')}">
    <xsl:value-of select="substring-after($text, '=')" />
  </xsl:attribute>
</xsl:template>

If this is really what you want to do, then you can call it on your img
element using:

<xsl:template match="img">
  <xsl:call-template name="parse-pseudo-XML-content">
    <xsl:with-param name="text" select="@alt" />
  </xsl:call-template>
</xsl:template>


Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email
jeni.tennison@xxxxxxxxxxxxxxxx



 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.