xmlxslttextmatching

How to match a specific text in parent element - adding new child elements with same name, but different text


I just can't figure out how I become able to match a specified text. So I have the following xml-data. And I want to add more display-name depending what channel.

    <?xml version="1.0" encoding="UTF-8" ?>
    <!-- EPG XMLTV FILE CREATED BY Take-a-LUG TEAM- (c) 2020 Bastian Kleinschmidt -->
    <!-- created on 2022-01-09 23:17:01.019522 -->
    <tv generator-info-name="Takealug EPG Grabber Ver. 1.1.2+matrix" generator-info-url="https://github.com/DeBaschdi/service.takealug.epg-grabber">
            
        <channel id="ARD.de">
                    <display-name lang="de">Das Erste</display-name>          
        </channel>
        <channel id="ZDF.de">
                    <display-name lang="de">ZDF</display-name>
        </channel>
        <channel id="RTL.de">
                    <display-name lang="de">RTL</display-name>
        </channel>
        <channel id="Sat1.de">
                    <display-name lang="de">SAT.1</display-name>
         </channel>
        <channel id="Pro7.de">
                    <display-name lang="de">ProSieben</display-name>
        </channel>
        <channel id="Kabel.de">
                    <display-name lang="de">kabel eins</display-name>
        </channel>
 </tv>

As you can see I have a specified channel-id and display-name for every channel. Now I want to have f.e. additional to <display-name>Das Erste</display-name>, also <display-name>Das Erste HD</display-name>. For ZDF, ZDF HD, or for 'Kabel Eins' additionaly to the HD suffix auch 'Kabel 1' as display-name. And there are some other channels where I want to do some adjustments. I tried some things, f.e.:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="channel/display-name[contains('Das Erste')]">
        <xsl:copy>
   <display-name>Das Erste HD</display-name>
    </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

But nothing happens, so I think the key for the solution is matching the right channel.


Solution

  • Change:

    <xsl:template match="channel/display-name[contains('Das Erste')]">
    

    to:

    <xsl:template match="channel[contains(display-name, 'Das Erste')]">
    

    See:
    https://www.w3.org/TR/1999/REC-xpath-19991116/#predicates
    https://www.w3.org/TR/1999/REC-xpath-19991116/#function-contains