-6

convert following xml into name value pair..any structure

    <root>
    <abc>
    <element_1>"<value-of select='a'>"</element_1>
    <element_2>"<value-of select='b'>"</element_2>
    </abc>
    <xyz>
    <element_3>"<value-of select='c'>"</element_3>
    </xyz>
    <element_4>"<value-of select='d'>"</element_4>
    </root>
MGarg
  • 1
  • i need following output – MGarg Mar 17 '15 at 00:41
  • > desired output "root/abc/element_1" "root/abc/element_2" "root/xyz/element_3" "root/element_4" – MGarg Mar 17 '15 at 00:41
  • 2
    Please edit and reformat your answer and provide the following: The input, your attempted XSLT, and the desired output. – Dan Field Mar 17 '15 at 00:43
  • I am not sure whether its possible or not. XSLT which can convert any xml into name value pairs. where name has absolute path of the field. – MGarg Mar 17 '15 at 00:44

1 Answers1

0

Try this (borrowed quite a bit from this post: How do you output the current element path in XSLT?):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="text()"/>

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

    <xsl:template match="*">
        <xsl:element name="udf">
            <xsl:element name="name">
                <xsl:for-each select="ancestor-or-self::*">
                    <xsl:value-of select="concat('/',local-name())"/>
                    <xsl:if test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
                        <xsl:value-of select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/>
                    </xsl:if>
                </xsl:for-each>
            </xsl:element>

            <xsl:element name="value">
                <xsl:value-of select="./text()"/>
            </xsl:element>
        </xsl:element>    
        <xsl:apply-templates select="node()"/>

    </xsl:template>

</xsl:stylesheet>

input:

<document>
  <blah>asdf</blah>
  <blah>fdsa</blah>
  <blah2>asdf2</blah2>
  <blah3 />
  <blah4><blah5>test3</blah5><blah6/>mixed</blah4>
  <blah4><blah5>test4</blah5></blah4>
</document>

output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <udf>
      <name>/document</name>
      <value>
    </value>
   </udf>
   <udf>
      <name>/document/blah[1]</name>
      <value>asdf</value>
   </udf>
   <udf>
      <name>/document/blah[2]</name>
      <value>fdsa</value>
   </udf>
   <udf>
      <name>/document/blah2</name>
      <value>asdf2</value>
   </udf>
   <udf>
      <name>/document/blah3</name>
      <value/>
   </udf>
   <udf>
      <name>/document/blah4[1]</name>
      <value>mixed</value>
   </udf>
   <udf>
      <name>/document/blah4[1]/blah5</name>
      <value>test3</value>
   </udf>
   <udf>
      <name>/document/blah4[1]/blah6</name>
      <value/>
   </udf>
   <udf>
      <name>/document/blah4[2]</name>
      <value/>
   </udf>
   <udf>
      <name>/document/blah4[2]/blah5</name>
      <value>test4</value>
   </udf>
</root>

XSLTransform.net: http://xsltransform.net/jyH9rMu/2

Community
  • 1
  • 1
Dan Field
  • 20,885
  • 5
  • 55
  • 71