xml - XSLT help! Convert any structure into Name Value pair -


convert following xml 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> 

try (borrowed quite bit post: how output 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


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -