1

I have an XML I need to match one text node (my XSLT my xslt already does that), and then if the last 4 characters of the the attribute value is equal to something I need to add a new attribute into the tag.

This the XML that I have

<LT>
    <identifications>
        <identification title="Delivery1">
            <Direction name="DFD_45_FLO">
                <ready>yes</ready>
                <indice>1</indice>
            </Direction>
        </identification>
        <identification title="Delivery2">
            <Direction name="KJI_45_PTS">
                <ready>yes</ready>
                <indice>0</indice>
            </Direction>
            </identification>
        <identification title="Delivery3">
            <Direction name="DFASDF_552_FLO">
                <ready>yes</ready>
                <indice>0</indice>
            </Direction>
            </identification>
    </identifications>
</LT>

And I want to match yes and after that if the last characters of the attribute name is FLO_ then copy the entire node an add the attribute paid="check" to the tag direction, in that way I would have an output like :

<LT>
    <identifications>
        <identification title="Delivery1">
            <Direction name="DFD_45_FLO" paid="check">
                <ready>yes</ready>
                <indice>1</indice>
            </Direction>
        </identification>
        <identification title="Delivery3">
            <Direction name="DFASDF_552_FLO" paid="check">
                <ready>yes</ready>
                <indice>0</indice>
            </Direction>
            </identification>
    </identifications>
</LT>

I have this XSLT, and it already do the first part but I dont know how to match the last characters with the if.

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

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

    <xsl:template match='Direction[ready/text()="yes"]'>
    <xsl:if test="@name='last characters = FLO'"> THIS IS THE LINE I DONT KNOW HOW TO WRITE
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="paid">check</xsl:attribute>
        <xsl:apply-templates select="node()"/>
      </xsl:copy>
    </xsl:if>
    </xsl:template>

    
</xsl:stylesheet>
runy29
  • 61
  • 4

1 Answers1

1

You could try this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Skip all identification we are not intersted in-->
  <xsl:template match="identification"/>
    
  <!-- Filter with extra predicate [@name[substring(., string-length(.) - 3) ='_FLO']]] on those Direction/@name that are ending with '_FLO'-->
  <xsl:template match="identification[Direction[ready/text()='yes'][@name[substring(., string-length(.) - 3) ='_FLO']]]">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="node()"/>
      </xsl:copy>
  </xsl:template>
  
  <!-- Since the filtering happens at parent we can just put <xsl:attribute name="paid">check</xsl:attribute> here -->  
  <xsl:template match="Direction">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="paid">check</xsl:attribute>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19