Ven
Ven

Reputation: 85

Selecting XML based on Path using XSLT

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>

Upvotes: 1

Views: 277

Answers (3)

Sam
Sam

Reputation: 850

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

Upvotes: 1

Ajeet Singh
Ajeet Singh

Reputation: 1086

Check following code only:-

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

Upvotes: 0

Robby Cornelissen
Robby Cornelissen

Reputation: 97322

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>

Upvotes: 0

Related Questions