Find and Copy into another element

Questions about XML that are not covered by the other forums should go here.
bobyrou
Posts: 2
Joined: Wed Apr 11, 2018 5:06 pm

Find and Copy into another element

Post by bobyrou » Wed Apr 11, 2018 5:16 pm

Hi, I am trying to find a way where the value inside <contactphone>value</contactphone> to be copied to <custom name="cs_phone">paste here</custom>

Doing manual work seems to be taking several hours for me (thousand records). Is there a way I can automate this? Any help is much appreciated :D

Code: Select all


<listings>
<listing>
<title lang="en_US">Apartment</title>
<content lang="en_US">Apartments for Rent</content>
<category>Apartments</category>
<contactemail>test@cdv.com</contactemail>
<contactname>Prop</contactname>
<price>1</price>
<currency>EUR</currency>
<city_area> </city_area>
<city>Belfast</city>
<region>CoAntrim</region>
<countryId>UK</countryId>
<country>United Kingdom</country>
<contactphone>04422222222</contactphone>
<custom name="ad_from">Prop</custom>
<custom name="no_rooms">4</custom>
<custom name="area">0mp</custom>
<custom name="cs_phone"> </custom>
<image>https://test.com/a.jpg</image>
<datetime>2018-04-06 10:53:51</datetime>
</listing>
.
.
.
</listings>

Radu
Posts: 6582
Joined: Fri Jul 09, 2004 5:18 pm

Re: Find and Copy into another element

Post by Radu » Thu Apr 12, 2018 2:05 pm

Hi,

Oxygen has support to define custom XML refactoring actions based on XSLT:

https://www.oxygenxml.com/doc/versions/ ... tools.html

so maybe you can write an XSLT stylesheet to take care of the refactoring.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

bobyrou
Posts: 2
Joined: Wed Apr 11, 2018 5:06 pm

Re: Find and Copy into another element

Post by bobyrou » Thu Apr 12, 2018 3:26 pm

Thanks for your reply. I am not very tehnical but I have seen a similar request and your colleague Adrian provided a solution here: topic11030.html#p31805 - I tried the solution in my case but it didn't applied the copy/paste, unless I am doing something wrong? Below is the xsl file created for transformation:

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::contactphone">
<xsl:apply-templates mode="copy"
select="parent::contactphone/preceding-sibling::cs_phone[1]/image/text()"/>
</xsl:if>
<xsl:if test="not(parent::contactphone)">
<xsl:apply-templates mode="copy"/>
</xsl:if>
</xsl:copy>
</xsl:template>

<!-- Handle default matching -->
<xsl:template match="*"/>
</xsl:stylesheet>

Radu
Posts: 6582
Joined: Fri Jul 09, 2004 5:18 pm

Re: Find and Copy into another element

Post by Radu » Fri Apr 13, 2018 11:21 am

Hi,

An XSLT stylesheet which would do something like you want would look like this:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="contactphone">
<custom name="{local-name()}">
<xsl:apply-templates/>
</custom>
</xsl:template>
</xsl:stylesheet>
As you seem to have various use-cases which can be solved using XLST maybe you could consider following an XSLT tutorial:

https://www.w3schools.com/xml/xsl_intro.asp

because we cannot guarantee that we have the human resources to help with any XSLT-based question asked on our forum.
There is also a public XSLT users list on which you can register and ask generic XSLT-related questions:

http://www.mulberrytech.com/xsl/xsl-list/

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply