Page 1 of 1

Concat 2 element's values and revise them

Posted: Fri Jul 02, 2021 7:08 pm
by s0n1k
Hi community, new here but it seems I will stay for long!
Can someone help me if I can do "Concat 2 element's values and revise them" with XPATH refactoring?

before:

Code: Select all

<category id="1214554589" name="Cell phone screwdrivers"/>
      <category_idosell id="2898" path="Hardware &gt; Tools"/>
after:

Code: Select all

<category name="Hardware///Tools///Cell phone screwdrivers" />

Re: Concat 2 element's values and revise them

Posted: Fri Jul 02, 2021 10:25 pm
by xephon
Hi s0n1k,

you can use this snippet:

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"
    version="2.0">
    
    <xsl:template match="/">
        <xsl:element name="category">
            <xsl:attribute name="name">
                <xsl:value-of select="substring-before(//category_idosell[1]/@path, ' &gt; ')"/>
                <xsl:value-of select="'///'"/>
                <xsl:value-of select="substring-after(//category_idosell[1]/@path, ' &gt; ')"/>
                <xsl:value-of select="'///'"/>
                <xsl:value-of select="//category[1]/@name"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>
    
</xsl:stylesheet>

Re: Concat 2 element's values and revise them

Posted: Sat Jul 03, 2021 4:57 am
by s0n1k
at your code I get an error:

Code: Select all

A sequence of more than one item is not allowed as the first argument of fn:substring-before() ("Hardware > Tools", "Hardware > Tools")

Re: Concat 2 element's values and revise them

Posted: Sat Jul 03, 2021 6:27 am
by s0n1k
ok, got it. I have tested at only one <product> .... </product> and it worked. Just it deleted everything and got the category result only.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><category name="Hardware///Tools///Cell phone screwdrivers"/>
Can you show me the way on how to apply this snippet at an XML file that contains 1000 <product> .. </products> and I want to get the same products just the only change to be the category element with the newly created value for each product?

thank you in advance for your time!

Re: Concat 2 element's values and revise them

Posted: Sat Jul 03, 2021 7:19 am
by s0n1k
it seems I am close to a solution. I found a code but it only outputs the 1st node.
How can I make it loop through all nodes and apply the snippet?
My code now

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"
    version="2.0">
  
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="category/@name">
   
        <xsl:element name="category">
            <xsl:attribute name="name">
                <xsl:value-of select="substring-before(//category_idosell[1]/@path, ' &gt; ')"/>
                <xsl:value-of select="'///'"/>
                <xsl:value-of select="substring-after(//category_idosell[1]/@path, ' &gt; ')"/>
                <xsl:value-of select="'///'"/>
                <xsl:value-of select="//category[1]/@name"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>
    
</xsl:stylesheet>

Re: Concat 2 element's values and revise them

Posted: Wed Jul 07, 2021 7:38 am
by xephon
You match on category/@name. Maybe this is not what you want to do. Probably you need an <xsl:apply-templates/> somewhere. Difficult to say without seeing your full code.