1

i have a source xml, where i would like to select based on path i.e. from 2nd or 3rd xml node. I have tried with the Some XSLT code, but not getting exact output.

Input XML :

            <?xml version="1.0" encoding="UTF-8"?>
            <ns0:Header xmlns:ns0="http://xyz987.com">
                <Main>
                    <Parent2>
                        <Parent2>
                            <?xml version="1.0" encoding="UTF-8"?>
                            <Child1>
                                <GChild1>12</GChild1>
                                <Child2>
                                    <GChild2>12</GChild2>
                                </Child2>
                            </Child1>
                        </Parent2>
                    </Parent2>

                </Main>
            </ns0:Header>

target XML:

            <?xml version="1.0" encoding="UTF-8"?>
            <Child1>
                <GChild1>12</GChild1>
                <Child2>
                    <GChild2>12</GChild2>
                </Child2>
            </Child1>

Tried XSLT Code :

            <?xml version="1.0" encoding="UTF-8" ?>
            <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                <xsl:output method="xml"  omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
                <xsl:strip-space elements="*"/>
                <xsl:template match="/">

                    <xsl:apply-templates select="/Header/Parent2/Parent2"/>

                </xsl:template>

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

                <xsl:template match="*">
                    <xsl:element name="{local-name()}">
                        <xsl:copy-of select="@*" />
                        <xsl:apply-templates />
                    </xsl:element>
                </xsl:template>

            </xsl:transform>
3
  • Is that really your XML, because it is not valid to have a <?xml version="1.0" encoding="UTF-8"?> declaration in the middle of an XML document. It must only occur on the very first line. Or perhaps in your real XML it is wrapped in a CDATA section, which would make this a very different problem! Thanks! Commented Sep 17, 2019 at 10:16
  • Your Header element is in a namespace, whereas your stylesheet is looking for Header elements in no namespace., Commented Sep 17, 2019 at 11:52
  • Possible duplicate of XSLT select nodes with namespace Commented Sep 17, 2019 at 13:02

3 Answers 3

1
**You can try this**:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates select="descendant::Parent2/Parent2/*"/>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output is:

<?xml version="1.0" encoding="utf-8"?>
<Child1>
   <GChild1>12</GChild1>
   <Child2>
      <GChild2>12</GChild2>
   </Child2>
</Child1>
Sign up to request clarification or add additional context in comments.

Comments

0

This stylesheet will copy all the child nodes under /Header/Main/Parent2/Parent2:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns0="http://xyz987.com">
    <xsl:output method="xml"  omit-xml-declaration="no" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:copy-of select="ns0:Header/Main/Parent2/Parent2/*"/>
    </xsl:template>
</xsl:transform>

Output:

<Child1 xmlns:ns0="http://xyz987.com">
   <GChild1>12</GChild1>
   <Child2>
      <GChild2>12</GChild2>
   </Child2>
</Child1>

Comments

0

Check following code only:-

<xsl:template match="Header">
        <xsl:copy-of select="Main/Parent2/Parent2/*"/>
</xsl:template>

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.