xml - XSLT to convert all elements into attributes leaving one node as it is -


i have xml code:

    <getdocument xmlns:ns="http://ecm.thehartford.com/ecm_cmd_doc/2009-06">     <ns:ecm_cmd_doc>     <ns:ecm_application_name>gbcledm</ns:ecm_application_name>     <ns:ecm_operation>         <mode>synchronous</mode>         <name>view</name>     </ns:ecm_operation>     <ns:doc_selection>         <ns:doc_object>             <dctm_object_type>ecm_gbd_bill_corr_doc</dctm_object_type>             <ns:doc_attr>                 <attr_value>6762884</attr_value>                 <attr_name>bill_number</attr_name>             </ns:doc_attr>         </ns:doc_object>     </ns:doc_selection>     </ns:ecm_cmd_doc>     </getdocument> 

which want convert into:

  <getdocument xmlns:ns="http://ecm.thehartford.com/ecm_cmd_doc/2009-06">   <ns:ecm_application_name>gbcledm</ns:ecm_application_name>   <ns:ecm_cmd_doc>     <ns:ecm_operation mode="synchronous" name="view"/>     <ns:doc_selection>         <ns:doc_object dctm_object_type="ecm_gbd_bill_corr_doc">             <ns:doc_attr attr_value="6762884" attr_name="bill_number"/>         </ns:doc_object>     </ns:doc_selection>   </ns:ecm_cmd_doc>   </getdocument> 

that is, elements converted attributes, ns:ecm_application_name remain is. tried xslt code:

     <xsl:stylesheet version="1.0" xmlns:ns="http://ecm.thehartford.com/ecm_cmd_doc/2009-06" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:output omit-xml-declaration="yes" indent="yes"/>      <xsl:strip-space elements="*"/>      <xsl:template match="*">      <xsl:choose>      <xsl:when test="ns:ecm_application_name">      <xsl:copy-of select="ns:ecm_application_name"/>      </xsl:when>      </xsl:choose>      <xsl:copy>      <xsl:for-each select="@*|*[not(* or @*)]">      <xsl:attribute name="{name(.)}">      <xsl:value-of select="."/>      </xsl:attribute>      </xsl:for-each>      <xsl:apply-templates select="*[* or @*]|text()"/>      </xsl:copy>      </xsl:template>      </xsl:stylesheet> 

would appreciate on this.

perhaps work you. takes leaf nodes (elements no child elements of own) except ns:ecm_application_name , converts them attributes of parent.

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:ns="http://ecm.thehartford.com/ecm_cmd_doc/2009-06"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <!-- identity transform --> <xsl:template match="@*|node()">     <xsl:copy>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template>  <xsl:template match="*[not(*)]">     <xsl:attribute name="{local-name()}">         <xsl:value-of select="."/>     </xsl:attribute> </xsl:template>  <xsl:template match="ns:ecm_application_name" priority="1">     <xsl:copy-of select="."/> </xsl:template>  </xsl:stylesheet> 

result (different result posted)

<getdocument xmlns:ns="http://ecm.thehartford.com/ecm_cmd_doc/2009-06">    <ns:ecm_cmd_doc>       <ns:ecm_application_name>gbcledm</ns:ecm_application_name>       <ns:ecm_operation mode="synchronous" name="view"/>       <ns:doc_selection>          <ns:doc_object dctm_object_type="ecm_gbd_bill_corr_doc">             <ns:doc_attr attr_value="6762884" attr_name="bill_number"/>          </ns:doc_object>       </ns:doc_selection>    </ns:ecm_cmd_doc> </getdocument> 

--

afaict, following variation:

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:ns="http://ecm.thehartford.com/ecm_cmd_doc/2009-06"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <!-- identity transform --> <xsl:template match="@*|node()">     <xsl:copy>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template>  <xsl:template match="getdocument">     <xsl:copy>         <xsl:copy-of select="ns:ecm_cmd_doc/ns:ecm_application_name"/>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template>  <xsl:template match="*[not(*)]">     <xsl:attribute name="{local-name()}">         <xsl:value-of select="."/>     </xsl:attribute> </xsl:template>  <xsl:template match="ns:ecm_application_name" priority="1"/>  </xsl:stylesheet> 

will return expected result, whether design or coincidence.


Comments