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

Copy element content to another nested empty element

Subject: Copy element content to another nested empty element
From: TeleServices <teleservices@xxxxxxxxx>
Date: Thu, 1 May 2008 07:37:35 -0700
 Copy element content to another nested empty element
I am just getting started with XML/XSLT and I struggle sometimes with
the non-procedural thinking required (I am used to plain old "C"
programming).

What I am trying to do is take the content of a selected element (node
from "lists) and copy it into another nested empty "phrase" element.
The empty element references the "list" element using "name" and
"list" attributes.  Here is a tiny example of the source:

<?xml version="1.0" encoding="utf-8"?>
<report>
<lists>
  <list name="phrase-list1">
    <option id="value1">a simple phrase</option>
    <option id="value2">also a simple phrase</option>
    <option id="value3">could be a simple phrase</option>
    <option id="value4">another simple phrase</option>
  </list>
  <list name="phrase-list2">
    <option id="value1">a compound phrase element with what '<phrase
name="value3" list="phrase-list1"/>' (single quotes) inside</option>
    <option id="value2">the compound phrase element containing
'<phrase name="value4" list="phrase-list1"/>' (single quotes)</option>
    <option id="value3">multi-compound phrases with '<phrase
name="value1" list="phrase-list1"/>' and '<phrase name="value4"
list="phrase-list1"/>' or more</option>
    <option id="value4">a compound compound phrase element with
"<phrase name="value2" list="phrase-list2"/>" (double quotes)</option>
  </list>
</lists>

<h4>Simple Phrases</h4>
The first phrase element is <phrase name="value1"
list="phrase-list1"/>.  And the second phrase element is <phrase
name="value2" list="phrase-list1"/>, and the third phrase element
<phrase name="value3" list="phrase-list1"/> as well.
<h4>Compound Phrases</h4>
Things get tricky though when instead of <phrase name="value1"
list="phrase-list1"/>, we have <phrase name="value1"
list="phrase-list2"/> of it.  We could have <phrase name="value3"
list="phrase-list2"/> inside of it.  We could even have <phrase
name="value4" list="phrase-list2"/> in a nested fashion.
</report>

The XSLT I am using works for "top level" (non-nested) elements but I
just cannot seem to grasp how to get this to work with the nested
elements.  I have a vague notion that I should be dealing with node
sets here, but this is at the edge of my current understanding.  Here
is the simple (non-working) XSLT:

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

<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="report"/>

<xsl:template match="/report">
  <html>
  <head>
    <title>Phrase Insertion</title>
    <style>
      .phrase {background:yellow;}
      .phrase .phrase {background:cyan;}
      .phrase .phrase .phrase {background:magenta;}
    </style>
  </head>
  <body>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

 <!-- insert phrases -->
<xsl:template match="phrase">
  <xsl:variable name="phrase" select="@name"/>
  <xsl:variable name="list" select="@list"/>
  <span class="phrase"><xsl:value-of
select="//lists/list[@name=$list]/option[@id=$phrase]"/></span>
</xsl:template>

<!-- pass other elements -->
<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<!-- do not directly process lists -->
<xsl:template match="lists"/>
</xsl:stylesheet>

The match="phrase" template is the culprit.  I have tried
apply-templates in a few different ways, but I cannot seem to get at
the nested "phrases".

Here is the current (non-working) output:

<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<title>Phrase Insertion</title>
<style>
      .phrase {background:yellow;}
      .phrase .phrase {background:cyan;}
      .phrase .phrase .phrase {background:magenta;}
    </style>
</head>
<body>
<h4>Simple Phrases</h4>
The first phrase element is <span class="phrase">a simple
phrase</span>.  And the second phrase element is <span
class="phrase">also a simple phrase</span>, and the third phrase
element <span class="phrase">could be a simple phrase</span> as well.
<h4>Compound Phrases</h4>
Things get tricky though when instead of <span class="phrase">a simple
phrase</span>, we have <span class="phrase">a compound phrase element
with what '' (single quotes) inside</span> of it.  We could have <span
class="phrase">multi-compound phrases with '' and '' or more</span>
inside of it.  We could even have <span class="phrase">a compound
compound phrase element with "" (double quotes)</span> in a nested
fashion.
</body>
</html>

NOTE: I have added a little CSS formatting to make it easy to see the
copied content.  Here is what I would like to produce:

<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
  <title>Phrase Insertion</title>
  <style>
    .phrase {background:yellow;}
    .phrase .phrase {background:cyan;}
    .phrase .phrase .phrase {background:magenta;}
  </style>
</head>
<body>
<h4>Simple Phrases</h4>
The first phrase element is <span class="phrase">a simple
phrase</span>.  And the second phrase element is <span
class="phrase">also a simple phrase</span>, and the third phrase
element <span class="phrase">could be a simple phrase</span> as well.
<h4>Compound Phrases</h4>
Things get tricky though when instead of <span class="phrase">a simple
phrase</span>, we have <span class="phrase">a compound phrase element
with what '<span class="phrase">could be a simple phrase</span>'
(single quotes) inside</span> of it.  We could have <span
class="phrase">multi-compound phrases with '<span class="phrase">a
simple phrase</span>' and '<span class="phrase">another simple
phrase</span>' or more</span> inside of it.  We could even have <span
class="phrase">a compound compound phrase element with "<span
class="phrase">the compound phrase element containing '<span
class="phrase">another simple phrase</span>' (single quotes)</span>"
(double quotes)</span> in a nested fashion.
</body>
</html>

If you copy and paste the two htm sections into files and view with a
browser it is very clear what I am getting versus what I am trying to
produce.

As I said I am thinking that I need to process this using nodesets,
but at this point "my reach exceeds my grasp", so if anyone can help
me understand the "trick" to dealing with the nested elements I would
appreciate it greatly.  I have searched this mailing list and the web
in general but I cannot not seem to find anything to help me in this
matter.

Thanks in advance for any help...


-Kiel

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.