-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.

Stan
  • 1
  • 2
  • 1
    You want to do an XSLT transform. What have you tried? Tools like https://www.freeformatter.com/xsl-transformer.html might help you get started. –  Feb 13 '18 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? – wasmachien Feb 13 '18 at 14:53
  • @wasmachien - I have edited the previous post to show exactly how the output xml should look like – Stan Feb 13 '18 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. – Stan Feb 13 '18 at 17:22
  • @Stan are you sure that's what you want? Because that is invalid XML. – wasmachien Feb 13 '18 at 17:23
  • @wasmachien - Its not invalid. The whole input XML should be converted to a String and stored as a value in the attribute. – Stan Feb 13 '18 at 17:26
  • Well, you will probably have to escape the attribute Strings, which will be part of the job of the transform. –  Feb 13 '18 at 17:30
  • Related: https://stackoverflow.com/q/1255372/1531971 This problem is going to be like using regexes, but with even more problems. –  Feb 13 '18 at 17:35
  • @Stan it is invalid, because quotes and <'s have to be escaped. But see my solution below and check if that works. – wasmachien Feb 13 '18 at 17:45
  • @JDK - If you think this approach is going to leave us with issues then i would like to do this conversion through Java code. Converting through an XSL would have made my design better. Anyway, thanks Jdv and wasmachien for your help. – Stan Feb 13 '18 at 17:47

1 Answers1

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>
wasmachien
  • 969
  • 1
  • 11
  • 28
  • 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. – Stan Feb 13 '18 at 17:50