Find and Copy into another element?

Questions about XML that are not covered by the other forums should go here.
kiai
Posts: 8

Find and Copy into another element?

Wed Oct 22, 2014 3:45 am

Is there a way to copy data from one element and paste the data into another element?

For example, how can the image in <page1> be copied into <page2> programmatically?

Code: Select all

<object>
   <page1>
      <image>pathtoimage/image-to-copy.png</image>
   </page1>
   <page2>
      <image></image>
   </page2>
   <pag
</object>


Thanks!
kiai
Posts: 8

Re: Find and Copy into another element?

Wed Oct 22, 2014 7:38 pm

Does this have to be done with an I/O parser?
adrian
Posts: 2395

Re: Find and Copy into another element?

Wed Oct 22, 2014 8:33 pm

Hi,

Are you looking for an XSLT or XQuery solution that does this, or a programming language in particular?
What tools do you have at hand? Do you want to do this within Oxygen or do you want to automate this independently from it?

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
kiai
Posts: 8

Re: Find and Copy into another element?

Wed Oct 22, 2014 9:59 pm

I'll try anything. Would like to see if there's a way to do it in oXygen. Can't seem to find a solution searching for Terminal commands. Thanks!
kiai
Posts: 8

Re: Find and Copy into another element?

Thu Oct 23, 2014 2:08 am

I was hoping that oXygen had a way to find data in one XPath and replace the data in another XPath.
adrian
Posts: 2395

Re: Find and Copy into another element?

Tue Oct 28, 2014 4:22 pm

Hi,

You can use an XSL script that copies the content from the XML source and adjusts what you need in the output.
e.g. XSL that for the image in page2 copies the image path from the preceding page1/image (XSL is based on 'samples/xhtml/copy.xsl')

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <!-- Match document -->
    <xsl:template match="/">
        <xsl:apply-templates mode="copy" select="."/>
    </xsl:template>
    <!-- Deep copy template -->
    <xsl:template match="*|text()|@*" mode="copy">
        <xsl:copy>
            <xsl:apply-templates mode="copy" select="@*"/>
            <xsl:apply-templates mode="copy"/>
        </xsl:copy>
    </xsl:template>
   
    <xsl:template match="image" mode="copy">
        <xsl:copy>
            <xsl:apply-templates mode="copy" select="@*"/>
            <xsl:if test="parent::page2">
                <xsl:apply-templates mode="copy"
                    select="parent::page2/preceding-sibling::page1[1]/image/text()"/>
            </xsl:if>
            <xsl:if test="not(parent::page2)">
                <xsl:apply-templates mode="copy"/>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
   
    <!-- Handle default matching -->
    <xsl:template match="*"/>
</xsl:stylesheet>

Save this in an .xsl file. To apply this in Oxygen you need to configure and run a transformation scenario.
Open the XML file and from the main menu invoke Document -> Transformation -> "Configure Transformation Scenario(s)" (there's a corresponding button in the toolbar)
In the Configure Transformation Scenario(s) dialog press New and select XML transformation with XSLT to create a new scenario:
1. Give it an appropriate name
2. Leave the XML URL field to its default(${currentFileURL})
3. In the XSL URL field browse for the stylesheet.
4. This is a '1.0' stylesheet, so you can leave the Transformer set to Saxon6.5.5.
5. You can further tune the Output. Please note that the Save as field must refer a single file, NOT an output directory. Use the editor variables to compose a generic name instead of a fixed one.
e.g in the "Save as" field you can specify: ${cfd}/${cfn}-out.xml which translates into <current-file-directory>/<current-filename>-out.xml
6. Press OK in the editing dialog and "Apply associated" to run it.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 1 guest