0

Is it possible to write xml within an xml element using XSLT.

Since im already within an element im unable to use match template function. I am trying to use xsl copy of but it results in the following error

an item of type 'element' cannot be constructed within a node of type 'attribute'

XML Format pre Transformation

<Results>
<Locations>
  <Location>
    <xyz>asa</xyz>
     <extended>
        <abc>blah</abc>
     </extended>
     <another>
        <abc>blah</abc>
     </another>
   </Location>
     <Location>
    <xyz>asa</xyz>
     <extended>
        <abc>blah</abc>
     </extended>
     <another>
        <abc>blah</abc>
     </another>
   </Location>
</Locations>
</Results>

** Desired XML Format After Transformation**

 <TransactionResponse ResponseType="Location_Query_Response1">

 <ParsedData Name="MessageData" Value="&#xA;&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;&#xD;*******************************Location*******"/>

  <Location>
    <xyz>asa</xyz>
     <extended>
        <abc>blah</abc>
     </extended>
     <another>
        <abc>blah</abc>
     </another>
   </Location>
     <Location>
    <xyz>asa</xyz>
     <extended>
        <abc>blah</abc>
     </extended>
     <another>
        <abc>blah</abc>
     </another>
     </Location>

  </TransactionResponse>

XSLT

<xsl:template match="/">
   <xsl:element name="TransactionResponse">
         <xsl:element name="Data">
              <xsl:element name="ParsedData">
                    this piece works
               </xsl:element>

           <xsl:for-each select="Locations/Location/*>
                <xsl:copy-of select="Location"/>

           </xsl:for-each>

          </xsl:element>
   </xsl:element>

1 Answer 1

0

Simply add another template to re-write Locations, avoiding <xsl:for-each> on all child nodes. Also, no need to use <xsl:element> as you can directly use node.

<xsl:template match="/">
   <TransactionResponse>
          <Data>
               <ParsedData>
                    this piece works
               </ParsedData>

               <xsl:apply-templates select="Locations"/>

          </Data>
   </TransactionResponse>
</xsl:template>

<xsl:template match="Locations">
   <xsl:copy-of select="*"/>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.