Question

This code is able to change elements to attribute key/value pairs. I want to change the attribute names in the same logic. How to do that?

Input:

<TESTSEQUENCE> 
  <TESTCASESTARTTIME>2014-05-02 16:02:38</TESTCASESTARTTIME> 
  <TESTCASENAME>TRAFFIC_MIXED_COLD_TP-A15 </TESTCASENAME> 
  <SCRIPTFILENAME>/proj/cpptemp/dt/network_repo/398/int/IEXS_CRX901185_1/iov/cbm1-3_mct/tc/Traffic.tcl</SCRIPTFILENAME> 
  <TXTLOGFILENAME>TRAFFIC_MIXED_COLD_TP-A15/TRAFFIC_MIXED_COLD_TP-A15.txt</TXTLOGFILENAME> 
  <HTMLLOGFILENAME>TRAFFIC_MIXED_COLD_TP-A15.html</HTMLLOGFILENAME> 
  <XMLLOGFILENAME>TRAFFIC_MIXED_COLD_TP-A15.xml</XMLLOGFILENAME>
  <STARTTIME>1399039358</STARTTIME> 
  <TOTALCMDS>27</TOTALCMDS>
  <NOTOKIDS>27</NOTOKIDS> 
  <NOTOKCMDS>{CETP ABORT}</NOTOKCMDS> 
  <FAILURESTRINGS>1=Failed,</FAILURESTRINGS> 
  <RESULT>ABORTED</RESULT> 
  <FINISHTIME>1399039672</FINISHTIME> 
</TESTSEQUENCE>

Stylesheet:

<xsl:template match="TESTSEQUENCE">
    <testcase>
      <xsl:for-each select="*">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="text()"/>
        </xsl:attribute>
      </xsl:for-each>
    </testcase>
</xsl:template>

Output:

<testcase time="2014-05-02 16:02:38" name="TRAFFIC_MIXED_COLD_TP-A15 " scriptfilename="mct/tc/Traffic.tcl" txtlogfilename="TRAFFIC_MIXED_COLD_TP-A15.txt" url="TRAFFIC_MIXED_COLD_TP-A15.html" xmllogfilename="TRAFFIC_MIXED_COLD_TP-A15.xml" starttime="1399039358" target_connect="" totalcmds="27" notokids="27" notokcmds="{CETP ABORT}" failurestrings="1=Failed," status="ABORTED" finishtime="1399039672">
</testcase>
Was it helpful?

Solution 2

TESTCASESTARTTIME to time TESTCASENAME to name HTMLLOGFILENAME to url RESULT to status I need these 4 attributes to be changed

You need to exclude these 4 elements from the generic xsl:for-each applied to the other elements, and handle them explicitly:

XSLT 1.0

<?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:template match="/">
<xsl:for-each select="TESTSEQUENCE">
    <testcase>
        <xsl:for-each select="*[not (self::TESTCASESTARTTIME or self::TESTCASENAME or self::HTMLLOGFILENAME or self::RESULT)]">
            <xsl:attribute name="{name()}">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </xsl:for-each> 
        <xsl:attribute name="time">
            <xsl:value-of select="TESTCASESTARTTIME"/>
        </xsl:attribute>
        <xsl:attribute name="name">
            <xsl:value-of select="TESTCASENAME"/>
        </xsl:attribute>
        <xsl:attribute name="url">
            <xsl:value-of select="HTMLLOGFILENAME"/>
        </xsl:attribute>
        <xsl:attribute name="status">
            <xsl:value-of select="RESULT"/>
        </xsl:attribute>
    </testcase>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Applied to your example input of:

<TESTSEQUENCE> 
  <TESTCASESTARTTIME>2014-05-02 16:02:38</TESTCASESTARTTIME> 
  <TESTCASENAME>TRAFFIC_MIXED_COLD_TP-A15</TESTCASENAME>   
  <SCRIPTFILENAME>/proj/cpptemp/dt/network_repo/398/int/IEXS_CRX901185_1/iov/cbm1-3_mct/tc/Traffic.tcl</SCRIPTFILENAME> 
  <TXTLOGFILENAME>TRAFFIC_MIXED_COLD_TP-A15/TRAFFIC_MIXED_COLD_TP-A15.txt</TXTLOGFILENAME> 
  <HTMLLOGFILENAME>TRAFFIC_MIXED_COLD_TP-A15.html</HTMLLOGFILENAME> 
  <XMLLOGFILENAME>TRAFFIC_MIXED_COLD_TP-A15.xml</XMLLOGFILENAME>
  <STARTTIME>1399039358</STARTTIME> 
  <TOTALCMDS>27</TOTALCMDS>
  <NOTOKIDS>27</NOTOKIDS> 
  <NOTOKCMDS>{CETP ABORT}</NOTOKCMDS> 
  <FAILURESTRINGS>1=Failed,</FAILURESTRINGS> 
  <RESULT>ABORTED</RESULT> 
  <FINISHTIME>1399039672</FINISHTIME> 
</TESTSEQUENCE>

the result is:

<?xml version="1.0" encoding="UTF-8"?>
<testcase SCRIPTFILENAME="/proj/cpptemp/dt/network_repo/398/int/IEXS_CRX901185_1/iov/cbm1-3_mct/tc/Traffic.tcl" TXTLOGFILENAME="TRAFFIC_MIXED_COLD_TP-A15/TRAFFIC_MIXED_COLD_TP-A15.txt" XMLLOGFILENAME="TRAFFIC_MIXED_COLD_TP-A15.xml" STARTTIME="1399039358" TOTALCMDS="27" NOTOKIDS="27" NOTOKCMDS="{CETP ABORT}" FAILURESTRINGS="1=Failed," FINISHTIME="1399039672" time="2014-05-02 16:02:38" name="TRAFFIC_MIXED_COLD_TP-A15" url="TRAFFIC_MIXED_COLD_TP-A15.html" status="ABORTED"/>

OTHER TIPS

Please enhance your code:

<xsl:template match="TESTSEQUENCE">
    <testcase>
      <xsl:for-each select="*">
        <xsl:attribute name="{concat('ABC-',name())}">
          <xsl:value-of select="text()"/>
        </xsl:attribute>
      </xsl:for-each>
    </testcase>
  </xsl:template>

OR

<xsl:template match="TESTSEQUENCE">
    <testcase>
      <xsl:for-each select="*">
        <xsl:attribute name="{if (name()) = 'TESTCASESTARTTIME' then 'TESTCASES' else ()}">
          <xsl:value-of select="text()"/>
        </xsl:attribute>
      </xsl:for-each>
    </testcase>
  </xsl:template>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top