xsl-list
[Top] [All Lists]

Re: variable matching...

2003-12-08 05:13:40


James Cummings wrote:
Hi there,

Let's say I have many different files, all with similar-but-differing
markup all of which I'm converting to the same structure.  In this case,
let's say I'm looking at plays so everything has the same basic
structure (acts, scenes, speeches, lines, stage directions).  Is there
a way of using variables (or parameters) to make transforming the names
easier without continually editing the main templates throughout the
xslt.

file a:
<body>
<title>A test file</title>
<Act><head>Act 1</head>
  <scene>
    <sd>Enter somebody</sd>
     <speech actor="foo">
      <line>This is a line</line>...
     </speech>
  </scene>...
</Act>...
</body>

file b:
<body>
<t>A test file</t>
<A><h1>Act 1</h1>
  <scn>
    <stagedir>Enter somebody</stagedir>
     <lines actor="foo">
      <l>This is a line</l>...
     </lines>
  </scn>...
</A>...
</body>

These have basically the same structure, but with differing element
names.

Is there a way for me to do something like:

<xsl:variable name="title">title</xsl:variable>
<xsl:variable name="act">Act</xsl:variable>
<xsl:variable name="scene">scene</xsl:variable>
<xsl:variable name="stage">sd</xsl:variable>

for the first file and then change the xsl it (or
pass these as parameters instead):

<xsl:variable name="title">t</xsl:variable>
<xsl:variable name="act">A</xsl:variable>
<xsl:variable name="scene">scn</xsl:variable>
<xsl:variable name="stage">stagedir</xsl:variable>

And do something like

<xsl:template match="$stage">
<stage><xsl:apply-templates/></stage>
</xsl:template>

I think this is not allowed, but was wondering if
there was a way (xslt2 acceptable) to do this kind
of thing?  I suppose instead of variables or parameters,
a secondary xml file would be another option.

Obviously I don't want to just do:
<xsl:template match="sd|stagedir">
<stage><xsl:apply-templates/></stage>
</xsl:template>
because sometimes one file will use an element name
that conflicts with another file's use of it.

I'm really looking for an easy way to not have to
customise the main bit of the xslt for each and every
file.

Suggestions?

Yes :)
You can work with just one xml format and write the xsl file for these
format (ie, fix reference elements).
Then other xsl files would transform other formats into your reference
format. I think it's very easy to maintain and extend.

For intance,
your reference element can be stagedir. Then a preprocessing phasis
would translate st element in stagedir elements. When this is done, you
can apply the core xsl stylesheet without having to match all the 
cases...
All variation on the name of the element would have a consequence on the
preprocessing phasing, leaving the core transform safe.

just a suggestion...

HTH

-- 
XPath free testing software :  http://lantern.sourceforge.net
Frédéric Laurent                     http://www.opikanoba.org

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



<Prev in Thread] Current Thread [Next in Thread>