[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Referencing content in XML and then processing in XSLT 2.0


Subject: Re: [xsl] Referencing content in XML and then processing in XSLT 2.0
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Tue, 23 Mar 2010 08:17:37 +0000

On 23 March 2010 07:53, Jacobus Reyneke <jacobusreyneke@xxxxxxxxx> wrote:
> Good day,
>
> If I am starting with a clean slate, and have the opportunity of
> controlling both the format of the XML and the XSLT, what would be the
> best way to achieve maintainable cross referencing within documents. I
> need to include content on the fly from other parts of the document,
> such as names, definitions, etc. One problem is that some of the cross
> referenced content needs to be included into attributes, and that is
> where I got my brain fried. I'm currently sitting with a hacked
> together solution that is frustrating the living daylights out of me,
> so I'm starting fresh!
>
> XML content is similar to this:
>
> <root>
>  <nuttycompany name="Gone Nuts" slogan="Gone Nuts is amazing">
>     <description>
>       At <<this company name>> we always do our best to give you yummy nuts
>     </description>
>  </nuttycompany>
>  <coatedcompany name="Coated Nuts" slogan="We are what
> <<nuttycompany's name>> has been missing all along">
>     <description>
>       At <<this company name>> we say that a good nut is a sweet nut.
> <<nuttycompany's name>> may say: "<<nuttycompany's slogan>>", but we
> don't believe it's the truth.
>     </description>
>  </coatedcompany>
> </root>
>
> Output needs to be:
>
> <root>
>  <nuttycompany name="Gone Nuts" slogan="Gone nuts is amazing">
>     <description>
>       At Gone Nuts we always do our best to give you yummy nuts
>     </description>
>  </nuttycompany>
>  <coatedcompany name="Coated Nuts" slogan="We are what Gone Nuts has
> been missing all along">
>     <description>
>       At Coated Nuts we say that a good nut is a sweet nut. Gone Nuts
> may say: "At Gone Nuts we always do our best to give you yummy nuts",
> but we don't believe it's the truth.
>     </description>
>  </coatedcompany>
> </root>
>
> What would be the ideal XML source and ideal XSLT transformation to
> solve this type of problem. I have considered my own placeholder
> naming convention, custom XML references, XInclude,
> xsl:analyze-string, two pass approach, etc, but nothing seems elegant
> or correct. I'm not posting my 'solution' in fear of going down a
> tangent. I'm looking for the "right" way, and not a fix for the wrong
> way ;-)
>
> Any ideas thrown this way will be dearly appreciated.

Define companies using a company element with an id:

<company id="comp1" name="Some Company">
  ..more details about the company
</company>

then refer to that company with a company ref element:

<company-ref ref-id="comp1"/>

then in your XSLT, have a template that matches "company-ref" that
gets the "company" element's @name:

<xsl:template match="description/company-ref">
  <xsl:apply-templates select="key('company-by-id', @ref-id)/@name"/>
..


cheers
--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/


Current Thread
Keywords