Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle xml linked to another xml, and copy the entire xml structure under given element

I am working on XSLT, where I have to traverse among two xmls and copy the entire xml under any given tag.

Main XML:

    <Content xmlns="some name space">
    <message>abcd/<message>
    <group xlink:href="Some link"></group>

    </Content>

Linked XML:

    <Content xmlns="linked xml name space">

        <text>
                <strong xmlns="http://www.w3.org/1999/xhtml">Hello</strong>


                    <br xmlns="http://www.w3.org/1999/xhtml"></br>

            1. Hi

                    <br xmlns="http://www.w3.org/1999/xhtml"></br>

            2. Hi all

                    <br xmlns="http://www.w3.org/1999/xhtml"></br>

            3. Bye
        </text>
    </Content>

I want to get the Entire xml structure under given xml element.

Required output.

    <AAA>
        <msg>abcd</msg>
        <data>
            <strong xmlns="http://www.w3.org/1999/xhtml">Hello</strong>


                <br xmlns="http://www.w3.org/1999/xhtml"></br>

        1. Hi

                <br xmlns="http://www.w3.org/1999/xhtml"></br>

        2. Hi all

                <br xmlns="http://www.w3.org/1999/xhtml"></br>

        3. Bye
        </data>
    </AAA>

XSLT written:

        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:simple="some name space" xmlns:link="linked xml name space" xmlns:xlink="http://www.w3.org/1999/xlink"  xmlns:xh="http://www.w3.org/1999/xhtml">
          <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
          <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
            <xsl:template match="/">
            <xsl:apply-templates />
          </xsl:template>

           <xsl:template match="simple:Content">
           <AAA>
                <msg>
                      <xsl:apply-templates select="simple:key" />
                </msg>
                <xsl:variable name="LINKED_COMPONENT" select="simple:group/document(@xlink:href)" />

                <data>
                    <xsl:apply-templates select="$LINKED_COMPONENT//link:text"/>
                </data>
            </AAA>
         </xsl:template>

            <xsl:template match="*">
            <xsl:copy>
              <!-- descend -->
              <xsl:apply-templates />
            </xsl:copy>
          </xsl:template>

          </xsl:stylesheet>

output got:

       <AAA>
            <msg>abcd</msg>
            <data>
               <text xmlns="linked xml name space">
                <strong xmlns="http://www.w3.org/1999/xhtml">Hello</strong>


                    <br xmlns="http://www.w3.org/1999/xhtml"></br>

            1. Hi

                    <br xmlns="http://www.w3.org/1999/xhtml"></br>

            2. Hi all

                    <br xmlns="http://www.w3.org/1999/xhtml"></br>

            3. Bye
            </text>
            </data>
        </AAA>

The "text" tags are also copied here. I dont want to copy those tags.

Can any one help me to correct it.

like image 599
Patan Avatar asked Dec 03 '25 16:12

Patan


1 Answers

Just change:

<xsl:apply-templates select="$LINKED_COMPONENT//link:text"/>

to:

<xsl:apply-templates select="$LINKED_COMPONENT//link:text/node()"/>
like image 79
Dimitre Novatchev Avatar answered Dec 06 '25 16:12

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!