I am trying to change category "name" values in all nodes in this XML sample
<?xml version="1.0" encoding="utf-8"?>
<offer xmlns:iof="********" xmlns:iaiext="******" file_format="IOF" generated="***" expires="***" version="3.0" extensions="yes">
<products currency="EUR" iof_translation_generated="no" language="eng">
<product id="170" currency="EUR" code_on_card="******" producer_code_standard="GTIN13" type="regular" vat="23.0" site="1">
<producer id="1404978613" name="Test"/>
<category id="1214554589" name="Cell phone screwdrivers"/>
<category_idosell id="2898" path="Hardware > Tools"/>
<unit id="0" name="pc."/>
<warranty id="2" type="seller" period="12" name="Cell phone accessories"/>
<price gross="1.1" net="0.9"/>
</product>
<product id="411" currency="EUR" code_on_card="******" producer_code_standard="OTHER" type="regular" vat="23.0" site="1">
<producer id="1404978613" name="Test"/>
<category id="1214554594" name="Service Tools"/>
<category_idosell id="2898" path="Hardware > Tools"/>
<unit id="0" name="pc."/>
<warranty id="1" type="seller" period="6" name="Cell phone spare parts"/>
<price gross="10.84" net="8.82"/>
</product>
</products>
</offer>
with this XSL snippet:
<?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, ' > ')"/>
<xsl:value-of select="'///'"/>
<xsl:value-of select="substring-after(//category_idosell[1]/@path, ' > ')"/>
<xsl:value-of select="'///'"/>
<xsl:value-of select="//category[1]/@name"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
It works when I use it in a single node but when I have more nodes (products) it give me this error:
A sequence of more than one item is not allowed as the first argument of fn:substring-before() ("Hardware > Tools", "Hardware > Tools")
can someone help me with what do I miss and how to make it work? thank you in advance
Your example is a bit ambiguous, because both products have the same value in category_idosell/@path
. AFAICT, you want to do simply:
<xsl:template match="category/@name">
<xsl:attribute name="name">
<xsl:value-of select="substring-before(../../category_idosell/@path, ' > ')"/>
<xsl:text>///</xsl:text>
<xsl:value-of select="substring-after(../../category_idosell/@path, ' > ')"/>
<xsl:text>///</xsl:text>
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
or even more simply:
<xsl:template match="category/@name">
<xsl:attribute name="name">
<xsl:value-of select="replace(../../category_idosell/@path, ' > ', '///')"/>
<xsl:text>///</xsl:text>
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>