Re: [xsl] Create XML from XPath expressions

Subject: Re: [xsl] Create XML from XPath expressions
From: Michael Müller-Hillebrand <mmh@xxxxxxxxxxxxx>
Date: Tue, 18 Aug 2009 16:39:14 +0200
Am 18.08.2009 um 15:12 schrieb Heiko Niemann:

Anyhow, I was looking for a way (an easy way)
to transform the Path data into a document structure, since I need the
whole branch and not just a single element, which I would get if I
applied
the (evaluated) XPath on the original document (e.g. copy-of).


Since the depth of the structure is not know it involves recursion.
For creating a new structure out of your DIFF data try this (thanks to
Kernow!):

<xsl:template match="Difference">
  <xsl:variable name="steps" select="tokenize(Path, '[/]')[.]"/>
  <xsl:call-template name="create-element">
    <xsl:with-param name="list" select="$steps"/>
    <xsl:with-param name="content" select="NewValue"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="create-element">
  <xsl:param name="list"/>
  <xsl:param name="content"/>
  <xsl:choose>
    <xsl:when test="count($list) = 2 and starts-with($list[2], '@')">
      <xsl:element name="{$list[1]}">
        <xsl:attribute name="{substring-after($list[2], '@')}"
select="$content"/>
      </xsl:element>
    </xsl:when>
    <xsl:when test="count($list) = 1">
      <xsl:element name="{$list[1]}">
        <xsl:value-of select="$content"/>
      </xsl:element>
    </xsl:when>
    <xsl:otherwise>
      <xsl:element name="{$list[1]}">
        <xsl:call-template name="create-element">
          <xsl:with-param name="list" select="remove($list, 1)"/>
          <xsl:with-param name="content" select="$content"/>
        </xsl:call-template>
      </xsl:element>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

to transform your input

<XmlDiffResult>

<Difference>
<Type>update</Type>
<Path>/item/street</Path>
<NodeType>Element</NodeType>
<NodeName>street</NodeName>
<NewValue>2020 Washington Ave.</NewValue>
</Difference>

<Difference>
<Type>insert</Type>
<Path>/item/street/@type</Path>
<NodeType>Attribute</NodeType>
<NodeName>type</NodeName>
<NewValue>business</NewValue>
</Difference>

<Difference>
<Type>update</Type>
<Path>/item/zip</Path>
<NodeType>Element</NodeType>
<NodeName>zip</NodeName>
<NewValue>90210</NewValue>
</Difference>

<Difference>
<Type>update</Type>
<Path>/item/city</Path>
<NodeType>Element</NodeType>
<NodeName>city</NodeName>
<NewValue>Los Angeles</NewValue>
</Difference>

</XmlDiffResult>

into

<XmlDiffResult>
  <item><street>2020 Washington Ave.</street></item>
  <item><street type="business"/></item>
  <item><zip>90210</zip></item>
  <item><city>Los Angeles</city></item>
</XmlDiffResult>

which can be stored in a variable and then unified, but only with the
indices you mention.

- Michael M|ller-Hillebrand

--
_______________________________________________________________
Michael M|ller-Hillebrand: Dokumentations-Technologie
Adobe Certified Expert, FrameMaker
Lvsungen und Training, FrameScript, XML/XSL, Unicode
Blog: http://cap-studio.de/ - Tel. +49 (9131) 28747

Current Thread