Using oxy_replace() Function and Xpath to replace Uppercase

Questions about XML that are not covered by the other forums should go here.
gg7aph
Posts: 3
Joined: Tue Feb 18, 2020 5:29 pm

Using oxy_replace() Function and Xpath to replace Uppercase

Post by gg7aph » Tue Feb 18, 2020 5:43 pm

Hello
Is it possible to use a combination of the oxy_replace() Function and xPath to find and replace elements with lower case versions of themselves??

oxy_replace ( text , target , replacement )

text:
The text in which the replace will occur. In my example this is the xPath to each "label".

target:
The target string to be replaced. In my example this is the @title attribute, currently in UPPER CASE.

replacement:
The string replacement. In my example this is the @title attribute, in the desired lower case

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

Re: Using oxy_replace() Function and Xpath to replace Uppercase

Post by Radu » Wed Feb 19, 2020 3:10 pm

Hi,

Here is a sample XML document:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="test.css"?>
<root>
    <label id="labelID">some content TITLE after</label>
    <element title="TITLE" idref="labelID"/>
</root>
and CSS:

Code: Select all

* {
    display:block;
}

element:before {
    content: oxy_replace(oxy_xpath(oxy_concat('//label[@id="', attr(idref), '"]/text()')), attr(title), oxy_lowercase(attr(title)));
}
The CSS code demonstrates how you can have various CSS functions nest in each other.

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

gg7aph
Posts: 3
Joined: Tue Feb 18, 2020 5:29 pm

Re: Using oxy_replace() Function and Xpath to replace Uppercase

Post by gg7aph » Thu Feb 27, 2020 5:50 pm

Thank you Radu

But how do I actually run this function within OxyGen to convert the required elements to lowercase?

Thanks

adrian_sorop
Posts: 8
Joined: Wed Jun 22, 2016 2:48 pm

Re: Using oxy_replace() Function and Xpath to replace Uppercase

Post by adrian_sorop » Fri Feb 28, 2020 3:33 pm

Hello,

To change the values of the attributes a Custom XML Refactoring action can be used.

The XML Refactoring Operation Descriptor is:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<refactoringOperationDescriptor 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.oxygenxml.com/ns/xmlRefactoring" 
    id="lower-case-attribute-value" 
    name="Lower case attribute value">
    <description>Lower case the attribute value</description>    
    <script type="XSLT" href="lowercase-attribute-value.xsl"/>
    <category>Attributes</category>
    <parameters>
        <description>Lowercase the value of the attributes</description>        
        <attributeLocation name="attribute_xpath" useCurrentContext="true">
            <element label="Element">
                <description></description>
            </element>
            <attribute label="Attribute">
                <description/>
            </attribute>
        </attributeLocation>
    </parameters>
</refactoringOperationDescriptor>
The XSLT script is:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    xmlns:xr="http://www.oxygenxml.com/ns/xmlRefactoring" version="3.0">
    
    <!-- The XPath that identifies the attributes -->
    <xsl:param name="attribute_xpath" as="xs:string" required="yes"/>
    
    <xsl:variable name="attributes" as="attribute()*">
        <xsl:evaluate xpath="$attribute_xpath" as="attribute()*" context-item="/"/>
    </xsl:variable>
    
    <!-- A sequence with the local name of the attributes-->
    <xsl:variable name="aln" select="distinct-values($attributes/local-name())"/>
    
    <xsl:template match="/|*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- Update the attribute value -->
    <xsl:template match="@*[local-name()= $aln]">
            <xsl:attribute name="{local-name()}">
                <xsl:value-of select="lower-case(.)"/>
            </xsl:attribute>
    </xsl:template>
    
    <xsl:template match="text() | comment() | processing-instruction() | @*">
        <xsl:copy/>
    </xsl:template>
</xsl:stylesheet>
Save the the descriptor(lowercase-attribute-value.xml) and the xslt script (lowercase-attribute-value.xsl) in a directory from your local drive and then refer the folder in Preferences->"XML / XML Refactoring" Load additional refactoring operations from field.

Regards,
Adrian S
Adrian Sorop
<oXygen/> XML Editor
http://www.oxygenxml.com

gg7aph
Posts: 3
Joined: Tue Feb 18, 2020 5:29 pm

Re: Using oxy_replace() Function and Xpath to replace Uppercase

Post by gg7aph » Tue Mar 03, 2020 4:49 pm

Adrian this worked wonderfully! Thank you VERY much!

Post Reply