简体   繁体   中英

when replace the xml elements with the help of xsl stylesheet using java,not getting replaced

how to replace the child tag name with xsl. here the below is my structure of xml.

  <Checkpax xmlns="http://xml.api.com/test">
    <customerLevel>
        <customerDetails>
            <paxDetails>
                <surname>MUKHERJEE</surname>
                <type>A</type>
                <gender>M</gender>
            </paxDetails>
            <otherPaxDetails>
                <givenName>JOY</givenName>
                <title>MR</title>
                <age>11</age>
            </otherPaxDetails>
            <otherPaxDetails>
                <title>MR</title>
            </otherPaxDetails>
        </customerDetails>
        <staffDetails>
            <staffInfo/>
            <staffCategoryInfo>
                <attributeDetails>
                    <attributeType>NA</attributeType>
                </attributeDetails>
            </staffCategoryInfo>
        </staffDetails>
        <productLevel>
            <legLevel>
                <legLevelIndicator>
                    <statusDetails>
                        <indicator>abc</indicator>
                        <action>1</action>
                    </statusDetails>
                </legLevelIndicator>
            </legLevel>
        </productLevel>
        <CustomerLevel>
            <legLevel>
                <legLevelIndicator>
                    <statusDetails>
                        <indicator>cde</indicator>
                        <action>1</action>
                    </statusDetails>
                </legLevelIndicator>
            </legLevel>
        </CustomerLevel>
    </customerLevel>
</Checkpax>

The below is my XSL file

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="customerLevel/productLevel/legLevel/legLevelIndicator/statusDetails">
        <statusInformation>
            <xsl:apply-templates select="@*|node()" />
        </statusInformation>
    </xsl:template>
</xsl:stylesheet>

here the statusDetails name should be changed as staffInformation inside the ProductLevel/LeglevelIndicator . Kindly give me the suggestion for doing this.

The below is expected result

<Checkpax xmlns="http://xml.api.com/test">
        <customerLevel>
            <customerDetails>
                <paxDetails>
                    <surname>MUKHERJEE</surname>
                    <type>A</type>
                    <gender>M</gender>
                </paxDetails>
                <otherPaxDetails>
                    <givenName>JOY</givenName>
                    <title>MR</title>
                    <age>11</age>
                </otherPaxDetails>
                <otherPaxDetails>
                    <title>MR</title>
                </otherPaxDetails>
            </customerDetails>
            <staffDetails>
                <staffInfo/>
                <staffCategoryInfo>
                    <attributeDetails>
                        <attributeType>NA</attributeType>
                    </attributeDetails>
                </staffCategoryInfo>
            </staffDetails>
            <productLevel>
                <legLevel>
                    <legLevelIndicator>
                        <statusInformation>
                            <indicator>abc</indicator>
                            <action>1</action>
                        </statusInformation>
                    </legLevelIndicator>
                </legLevel>
            </productLevel>
            <CustomerLevel>
                <legLevel>
                    <legLevelIndicator>
                        <statusDetails>
                            <indicator>cde</indicator>
                            <action>1</action>
                        </statusDetails>
                    </legLevelIndicator>
                </legLevel>
            </CustomerLevel>
        </customerLevel>
    </Checkpax>

the statusDetails name should be changed as staffInformation inside the ProductLevel/LeglevelIndicator

Try it this way:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://xml.api.com/test"
exclude-result-prefixes="ns0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="ns0:productLevel/ns0:legLevel/ns0:legLevelIndicator/ns0:statusDetails">
    <staffInformation xmlns="http://xml.api.com/test">
        <xsl:apply-templates/>
    </staffInformation>
</xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM