Xsl for WTF "well-formed" example

I laughed out loud when I read this article on the Daily WTF: http://thedailywtf.com/Articles/WellFormed-XML.aspx , but now it's not funny because I started to recognize this “xml design template” in the wild with alarming frequency. for example, I just exported some data from a reasonable clearquest request, and I got the following:

<?xml version="1.0" encoding="us-ascii"?>
<?xml-stylesheet type="text/xsl" href="http://scm/rational/clearquest/webservice/resultset.xsl"?>
<resultset dbset="CQMaster" dbname="PROD" entitydefname="TR" count="1" name="_my trs">
  <header count="3">
    <column type="dbid">dbid</column>
    <column type="id">id</column>
    <column type="short_string">Abstract</column>
  </header>
  <record>
    <field>33607697</field>
    <field>PROD00011111</field>
    <field>The product has a bug that needs fixed.</field>
  </record>
</resultset>

I am not an xslt master - I will probably figure it out sooner or later, but this cannot stop asking ... what is the simplest xslt template to convert above to something more useful, like this:

<?xml version="1.0" encoding="us-ascii"?>
<resultset dbset="CQMaster" dbname="PROD" entitydefname="TR" count="1" name="_my trs">
  <record>
    <dbid type="dbid">33607697</dbid>
    <id type="id">PROD00011111</id>
    <Abstract type="short_string">The product has a bug that needs fixed.</Abstract>
  </record>
</resultset>
+3
source share
1 answer

The following conversion :

<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:key name="kColByPos" match="column"
  use="count(preceding-sibling::*) +1"/>

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates select="record"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="record">
  <record>
    <xsl:apply-templates/>
  </record>
 </xsl:template>

 <xsl:template match="field">
  <xsl:variable name="vColumn" select=
   "key('kColByPos', position())"/>

   <xsl:element name="{translate($vColumn, ' ', '_'}">
    <xsl:copy-of select="$vColumn/@type"/>
    <xsl:apply-templates/>
   </xsl:element>
 </xsl:template>
</xsl:stylesheet>

XML-:

<resultset dbset="CQMaster" dbname="PROD" entitydefname="TR" count="1" name="_my trs">
  <header count="3">
    <column type="dbid">dbid</column>
    <column type="id">id</column>
    <column type="short_string">Abstract</column>
  </header>
  <record>
    <field>33607697</field>
    <field>PROD00011111</field>
    <field>The product has a bug that needs fixed.</field>
  </record>
</resultset>

, :

<resultset dbset="CQMaster" dbname="PROD" entitydefname="TR" count="1" name="_my trs">
    <record>
        <dbid type="dbid">33607697</dbid>
        <id type="id">PROD00011111</id>
        <Abstract type="short_string">The product has a bug that needs fixed.</Abstract>
    </record>
</resultset>
+1

Source: https://habr.com/ru/post/1761169/


All Articles