Xslt convert xml string to xml elements

complicated here.

I have the following XML

<test>
     <testElement SomeAttribute="<otherxml><otherElement>test test</otherElement></otherxml>">
      </testElement>
</test>

Using XSLT, I want to convert this XML to have the following result.

<test>
     <testElement>
        <SomeAttributeTransformedToElement>
          <otherxml>
               <otherElement>test test</otherElement>
          </otherxml>
        </SomeAttributeTransformedToElement>
      </testElement>
</test>

Basically, some text in an attribute should be converted to actual elements in the final XML

Any ideas on how to achieve this in XSLT?

Alex

+3
source share
2 answers

You can achieve this by disabling output shielding. However, note that your input document is not a valid XML document (it <is illegal in attribute values ​​and requires escaping). Therefore, I modified your input document as follows:

Input document

<?xml version="1.0" encoding="utf-8"?>
<test>
  <testElement SomeAttribute="&lt;otherxml>&lt;otherElement>test test&lt;/otherElement>&lt;/otherxml>">
  </testElement>
</test>

XSLT

<?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" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@SomeAttribute">
    <SomeAttributeTransformedToElement>
      <xsl:value-of select="." disable-output-escaping="yes"/>
    </SomeAttributeTransformedToElement>
  </xsl:template>
</xsl:stylesheet>

, disable-output-escaping="yes" , XML-.

+3

, - .

. ( ), -, .

, XSL- :

XSLT named-template XML- . . :

  • XML (<a> <b> <a> ... </a> </b> </a> , "a ' ' a ')
  • XML ( , < elmt attr1 = "1" attr2 = "2" > , < elmt        ; attr1 = "1"   attr2 = "2" > ( )
  • & ltelement/" > (, , ), .

, : , , . , script, , .

:

<xsl:template name="t-convert">
    <xsl:param name="TEXT"/>
    <xsl:choose>
        <xsl:when test="starts-with($TEXT,'&lt;?')">
            <xsl:call-template name="t-convert">
                <xsl:with-param name="TEXT" select="substring-after($TEXT,'?&gt;')"/>
            </xsl:call-template>
        </xsl:when>
        <!-- Si le texte contient encore des elements -->
        <xsl:when test="contains($TEXT,'&lt;')">
            <xsl:variable name="before-first-open" select="substring-before($TEXT,'&lt;')"/>
            <xsl:variable name="after-first-open" select="substring-after($TEXT,'&lt;')"/>
            <!-- On ecrit le texte qui précéde l'élément -->
            <xsl:value-of select="$before-first-open"/>
            <!-- Le nom de l'élément -->
            <xsl:variable name="TRAD" select="translate($after-first-open,'&gt;',' ')"/>
            <!--  TODO : Gere le cas <ELEMENT />   -->
            <xsl:variable name="ELEMENT" select="substring-before($TRAD,' ')"/>
            <xsl:variable name="suite" select="substring-after($after-first-open,$ELEMENT)"/>
            <xsl:variable name="DEFINITION" select="substring-before($suite,'&gt;')"/>
            <xsl:variable name="CONTENT" select="substring-after(substring-before($suite,concat('&lt;/',$ELEMENT)),concat($DEFINITION,'&gt;'))"/>
            <xsl:variable name="FOLLOWING">
                <xsl:choose>
                    <xsl:when test="substring($DEFINITION,string-length($DEFINITION))='/'"><!--  ends-with($DEFINITION,'/') not compatible with all XSLT version -->
                        <xsl:value-of select="substring-after($suite,'/&gt;')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="substring-after(substring-after($suite,concat('&lt;/',$ELEMENT)),'&gt;')"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:element name="{$ELEMENT}">
                <xsl:call-template name="t-attribs">
                    <xsl:with-param name="TEXT" select="$DEFINITION"/>
                </xsl:call-template>
                <xsl:call-template name="t-convert">
                    <xsl:with-param name="TEXT" select="$CONTENT"/>
                </xsl:call-template>
            </xsl:element>
            <xsl:call-template name="t-convert">
                <xsl:with-param name="TEXT" select="$FOLLOWING"/>
            </xsl:call-template>
        </xsl:when>
        <!-- no more element -->
        <xsl:otherwise>
            <xsl:value-of select="$TEXT"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="t-attribs">
    <xsl:param name="TEXT"/>
    <xsl:if test="contains($TEXT,'=')">
        <!-- Assert TEXT=' NAME="VALUE".*' -->
        <xsl:variable name="NAME" select="substring-after(substring-before($TEXT,'='),' ')"/>
        <xsl:variable name="afterName" select="substring-after($TEXT,'=&quot;')"/>
        <xsl:variable name="VALUE" select="substring-before($afterName,'&quot;')"/>
        <xsl:variable name="FOLLOWING" select="substring-after($afterName,'&quot;')"/>
        <xsl:attribute name="{$NAME}">
            <xsl:value-of select="$VALUE"/>
        </xsl:attribute>
        <xsl:call-template name="t-attribs">
            <xsl:with-param name="TEXT" select="FOLLOWING"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

:

<xsl:call-template name="t-convert">
        <xsl:with-param name="TEXT" select="//content"/>
</xsl:call-template>

, ( !)

+1

Source: https://habr.com/ru/post/1766446/


All Articles