RE: [xsl] Need a programmic way to read xslt file, and determine the mappings defined.

Subject: RE: [xsl] Need a programmic way to read xslt file, and determine the mappings defined.
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Mon, 8 Nov 2004 15:44:19 -0000
> No, my desire is to get a xslt file and with java determine
> the mapping. Then determine how to reverse eng the process to
> find my source.
>
> Background: My app uses xslt to translate data model A to
> data model B. My user wishes to not to write certain
> attributes in a table in data model B.
> He selects the attributes to mask. I would then like to
> inform him of the source that was used to map to that target
> attribute, and inform him of what other targets that same
> source was mapped to.
>
> So I need to something to parse the xslt file, and expose
> interfaces that allow me to easily determine this mapping.

A technique I've used for a completely different requirement may be
useful here.  What you need to do is add a unique identifier to each
source element, then copy that through to the result.  When the user
'masks' of a section of the HTML, you can find out which unique
identifiers have been masked and then re-parse the source xml to locate
the source elements.

The unique I identifier I used was the element index, added using an
XMLFilter as the source is read in.  A separate XMLFilter adds an
attribute to every literal result element of the stylesheet as it gets
parsed to copy the element index through to the result.  For example:

Source XML:
<root>
  <foo/>
  <bar/>
</root>

Source XML after XMLFilter:
<root id="1">
  <foo id="2"/>
  <bar id="3"/>
</root>

Stylesheet:
<xsl:template match="foo">
  <div>
    <xsl:value-of select="."/>
  </div>
</xsl:template>

Stylesheet after XMLFilter:
<xsl:template match="foo">
  <div elementIndex="{@id}">
    <xsl:value-of select="."/>
  </div>
</xsl:template>

Result when applied:
<div elementIndex="2"/>

So now when your user selects an area of the screen you can access the
elementIndex attribute and then query the source xml for an element with
a matching number, and locate <foo/>.  That is, you know that that <div>
was generated from the template that processed the source <foo/>
element.

The element indexes and AVT's are all added at transform time using
XMLFilters so it keeps you source and stylesheets clean with a minimal
performance impact.

I've been a bit brief but I hope you get the idea,

Cheers
anderw

Current Thread