Page 1 of 1

Using oxy_replace() Function and Xpath to replace Uppercase

Posted: Tue Feb 18, 2020 5:43 pm
by gg7aph
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

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

Posted: Wed Feb 19, 2020 3:10 pm
by Radu
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

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

Posted: Thu Feb 27, 2020 5:50 pm
by gg7aph
Thank you Radu

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

Thanks

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

Posted: Fri Feb 28, 2020 3:33 pm
by adrian_sorop
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

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

Posted: Tue Mar 03, 2020 4:49 pm
by gg7aph
Adrian this worked wonderfully! Thank you VERY much!