[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Create XML from XPath expressions

Subject: Re: Create XML from XPath expressions
From: Michael Müller-Hillebrand <mmh@xxxxxxxxxxxxx>
Date: Tue, 18 Aug 2009 16:39:14 +0200
Re:  Create XML from XPath expressions
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.