I'm trying to change child values if sibling matches numbers in a list. this is my xml:
<interval>
<employee>
<userSign>505651</userSign>
</employee>
<homeCostCenter></homeCostCenter>
<intervalStart>2021-10-14</intervalStart>
<intervalEnd>2021-10-23</intervalEnd>
<entries>
<entry>
<entryStart>2021-10-14T00:00:00</entryStart>
<entryEnd>2021-10-14T00:00:00</entryEnd>
<type>R01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-15T00:00:00</entryStart>
<entryEnd>2021-10-15T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-16T00:00:00</entryStart>
<entryEnd>2021-10-16T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-17T00:00:00</entryStart>
<entryEnd>2021-10-17T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
</interval>
<interval>
<employee>
<userSign>506719</userSign>
</employee>
<homeCostCenter></homeCostCenter>
<intervalStart>2021-10-14</intervalStart>
<intervalEnd>2021-10-23</intervalEnd>
<entries>
<entry>
<entryStart>2021-10-14T00:00:00</entryStart>
<entryEnd>2021-10-14T00:00:00</entryEnd>
<type>R01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-15T00:00:00</entryStart>
<entryEnd>2021-10-15T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-16T00:00:00</entryStart>
<entryEnd>2021-10-16T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
</interval>
I want to change the text of every "shiftGroup" child under this parent "interval" element to "Shift_A" only if "userSign" in ('505651','564125',...).
How can I do that on xslt?
Thanks.
CodePudding user response:
using xslt 2:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:param name="p" />
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:with-param name="p"><xsl:value-of select="$p"/></xsl:with-param>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="interval">
<xsl:param name="p" />
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:with-param name="p"><xsl:value-of select="employee/userSign"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="shiftGroup">
<xsl:param name="p" />
<shiftGroup>
<xsl:choose>
<xsl:when test="$p = (505651,564125)">
<xsl:text>Shift_A</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</shiftGroup>
</xsl:template>
</xsl:stylesheet>
output:
<list>
<interval>
<employee>
<userSign>505651</userSign>
</employee>
<homeCostCenter/>
<intervalStart>2021-10-14</intervalStart>
<intervalEnd>2021-10-23</intervalEnd>
<entries>
<entry>
<entryStart>2021-10-14T00:00:00</entryStart>
<entryEnd>2021-10-14T00:00:00</entryEnd>
<type>R01</type>
<shiftGroup>Shift_A</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-15T00:00:00</entryStart>
<entryEnd>2021-10-15T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>Shift_A</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-16T00:00:00</entryStart>
<entryEnd>2021-10-16T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>Shift_A</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-17T00:00:00</entryStart>
<entryEnd>2021-10-17T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>Shift_A</shiftGroup>
</entry>
</entries>
</interval>
<interval>
<employee>
<userSign>506719</userSign>
</employee>
<homeCostCenter/>
<intervalStart>2021-10-14</intervalStart>
<intervalEnd>2021-10-23</intervalEnd>
<entries>
<entry>
<entryStart>2021-10-14T00:00:00</entryStart>
<entryEnd>2021-10-14T00:00:00</entryEnd>
<type>R01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-15T00:00:00</entryStart>
<entryEnd>2021-10-15T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
<entry>
<entryStart>2021-10-16T00:00:00</entryStart>
<entryEnd>2021-10-16T00:00:00</entryEnd>
<type>O01</type>
<shiftGroup>SP27</shiftGroup>
</entry>
</entries>
</interval>
</list>