-1

How to convert an input XML to an attribute value of another XML using XSL?

Input XML:

<Order OrderNo="1" />

Output XML:

<NewXML Input="<Order OrderNo="1" />" />

I need the input XML to be converted as a string and stored in an attribute value in the output XML.

10
  • 1
    You want to do an XSLT transform. What have you tried? Tools like freeformatter.com/xsl-transformer.html might help you get started. Commented Feb 13, 2018 at 14:51
  • Your question is not entirely clear. What should the output XML look like exactly? And what part of the input XML do you want to see in it? Commented Feb 13, 2018 at 14:53
  • @wasmachien - I have edited the previous post to show exactly how the output xml should look like Commented Feb 13, 2018 at 17:20
  • @jdv - I have done other XSL conversions earlier, but i dont have a clue to convert the whole input XML to an attribute value of output XML. Commented Feb 13, 2018 at 17:22
  • @Stan are you sure that's what you want? Because that is invalid XML. Commented Feb 13, 2018 at 17:23

1 Answer 1

0

Converting an XML node to a string doesn't really make sense, and I'm pretty sure there is an easier way to do whatever you are trying to do. But here is a slightly dirty way to do it, which will convert elements and their attributes:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="Order">
        <NewXML>
            <xsl:attribute name="Input">
              <xsl:apply-templates select="." mode="element-to-string"/>
            </xsl:attribute>
        </NewXML>
  </xsl:template>

    <xsl:template match="*" mode="element-to-string">
        <xsl:text>&lt;</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:for-each select="@*">
            <xsl:value-of select="concat(' ', name(), '=&quot;', ., '&quot;')"/>
        </xsl:for-each>     
        <xsl:text>&gt;</xsl:text>
    </xsl:template>

  </xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be working in online converter. Let me test it in my system and will let you know. It will be awesome if it works. Thanks.

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.