Concat 2 element's values and revise them

Questions about XML that are not covered by the other forums should go here.
s0n1k
Posts: 4
Joined: Fri Jul 02, 2021 6:30 pm

Concat 2 element's values and revise them

Post by s0n1k » Fri Jul 02, 2021 7:08 pm

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" />

xephon
Posts: 107
Joined: Mon Nov 24, 2014 1:49 pm
Location: Greven/Germany

Re: Concat 2 element's values and revise them

Post by xephon » Fri Jul 02, 2021 10:25 pm

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>
Open-source DITA-XML solutions: https://github.com/doctales

s0n1k
Posts: 4
Joined: Fri Jul 02, 2021 6:30 pm

Re: Concat 2 element's values and revise them

Post by s0n1k » Sat Jul 03, 2021 4:57 am

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")

s0n1k
Posts: 4
Joined: Fri Jul 02, 2021 6:30 pm

Re: Concat 2 element's values and revise them

Post by s0n1k » Sat Jul 03, 2021 6:27 am

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!

s0n1k
Posts: 4
Joined: Fri Jul 02, 2021 6:30 pm

Re: Concat 2 element's values and revise them

Post by s0n1k » Sat Jul 03, 2021 7:19 am

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>

xephon
Posts: 107
Joined: Mon Nov 24, 2014 1:49 pm
Location: Greven/Germany

Re: Concat 2 element's values and revise them

Post by xephon » Wed Jul 07, 2021 7:38 am

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.
Open-source DITA-XML solutions: https://github.com/doctales

Post Reply