tags:

views:

188

answers:

1

Hello everybody!

At runtime I receive xml document and I want to display it somehow different in JSF. For example:

This:

<invoker.ArrayOfDictionary>
  <dictionary>
    <invoker.Dictionary>
      <id>gcide</id>
      <name>The Collaborative International Dictionary of English v.0.48</name>
    </invoker.Dictionary>
    <invoker.Dictionary>
      <id>wn</id>
      <name>WordNet (r) 2.0</name>
    </invoker.Dictionary>
    <invoker.Dictionary>
      <id>moby-thes</id>
      <name>Moby Thesaurus II by Grady Ward, 1.0</name>
    </invoker.Dictionary>

in this:

invoker.ArrayOfDictionary:
  dictionary:
    invoker.Dictionary:
      id:gcide
      name:The Collaborative International Dictionary of English v.0.48
    invoker.Dictionary:
      id:wn
      name:WordNet (r) 2.0
    invoker.Dictionary:
      id:moby-thes
      name:Moby Thesaurus II by Grady Ward, 1.0

I was thinking to do this with XSLT transformation. Some guidelines how to start with xslt? Or maybe you have another idea for this?

SOLVED:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"&gt;
        <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="vNL" select="'&#xA;'"/>
    <xsl:variable name="vSpaces" select="'          '"/>
    <xsl:template match="*">
       <xsl:value-of select="concat(
    $vNL,
    substring($vSpaces,1,count(ancestor::node())),
    $startBold,
    name(),
    ':')"/>
       <xsl:apply-templates/>
     </xsl:template>
    </xsl:stylesheet>

Thanks for the help!

+2  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my"
>
 <xsl:output method="text"/>

 <xsl:strip-space elements="*"/>

 <my:offsets>
   <offset value="0">
     <element name="invoker.ArrayOfDictionary"/>
   </offset>
   <offset value="2">
     <element name="dictionary"/>
   </offset>
   <offset value="4">
     <element name="invoker.Dictionary"/>
   </offset>
   <offset value="6">
     <element name="name"/>
     <element name="id"/>
   </offset>
 </my:offsets>

 <xsl:variable name="vOffsets"
      select="document('')/*/my:offsets"/>

 <xsl:variable name="vNL" select="'&#xA;'"/>

 <xsl:variable name="vSpaces"
      select="'          '"/>

 <xsl:template match="*">
   <xsl:variable name="vthisOffset" select=
   "$vOffsets/*
          [element/@name
          =
           name(current())
           ]
            /@value
   "/>
   <xsl:value-of select=
   "concat($vNL,
           substring($vSpaces,1,$vthisOffset),
           name(),
           ':'
           )"
   />
   <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document (corrected to be well-formed):

<invoker.ArrayOfDictionary>
  <dictionary>
    <invoker.Dictionary>
      <id>gcide</id>
      <name>The Collaborative International Dictionary of English v.0.48</name>
    </invoker.Dictionary>
    <invoker.Dictionary>
      <id>wn</id>
      <name>WordNet (r) 2.0</name>
    </invoker.Dictionary>
    <invoker.Dictionary>
      <id>moby-thes</id>
      <name>Moby Thesaurus II by Grady Ward, 1.0</name>
    </invoker.Dictionary>
  </dictionary>
</invoker.ArrayOfDictionary>

produces the wanted, correct result:

invoker.ArrayOfDictionary:
  dictionary:
    invoker.Dictionary:
      id:gcide
      name:The Collaborative International Dictionary of English v.0.48
    invoker.Dictionary:
      id:wn
      name:WordNet (r) 2.0
    invoker.Dictionary:
      id:moby-thes
      name:Moby Thesaurus II by Grady Ward, 1.0

Do note:

  1. The use of method="text" in the <xsl:output> instruction.

  2. All offsets are defined for all elements in a convenient tree, which can be maintained in a separate file and modified without having to touch the XSLT code.

Dimitre Novatchev
You can ask for the length of the path to the root for a given node to get the indentation generically.
Thorbjørn Ravn Andersen
@Thorbjørn-Ravn-Andersen: Yes, I know this more generic solution. However it is more applicable to more generalized formatting (such as XML formatting). Here we have text, and the shape is not generally known/assumed (e.g. tree). This is why I wanted to provide more flexibility for custom indentations. The other reason is that no XML Schema has been provided for the source XML document.
Dimitre Novatchev
thanks for the help!
Milan