1

I have following xml

<myRequest>
    <id>123456789</id>
</myRequest>

I have the following xslt not working to get the same as output

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="xml" indent="yes" encoding="ISO-8859-1" version="1.0"
        omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="myRequest">
        <myRequest>
            <xsl:apply-templates select="id"/>
        </myRequest>
    </xsl:template>

    <xsl:template match="id">
        <customerId>
            <xsl:value-of select="id"/>
        </customerId>
    </xsl:template>

</xsl:stylesheet>

working xslt

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="xml" indent="yes" encoding="ISO-8859-1" version="1.0"
        omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="myRequest">
        <myRequest>
            <xsl:apply-templates select="//id"/>
        </myRequest>
    </xsl:template>

    <xsl:template match="id">
        <customerId>
            <xsl:value-of select="//id"/>
        </customerId>
    </xsl:template>

</xsl:stylesheet>

I put current() in the XWatch,it is also not showing any thing when debug pointer is in

<xsl:template match="myRequest">
            <myRequest>
..........................

Why i need to use // here ?Because id element is directly under myRequest.I really got confused using // here?

Without using // also we need to get the output.

What the mistake i am doing here ?

Thanks in advance...

0

1 Answer 1

1

Your first template needs to look like this...

<xsl:template match="id">
    <customerId>
        <xsl:value-of select="."/>
    </customerId>
</xsl:template>

The expression in the xsl:value-of will be relative to the current node you are on (the id) node, so by doing <xsl:value-of select="id" /> you are looking for a node called id that is a child of the current id element. Doing . gets the value of the current node.

Doing //id works because when you start the expression with / it will be relative to the top-level document node, and doing // will then search for the first id anywhere in the document.

Note, consider building your XSLT upon the XSLT identity template, as that will make it much more generic, and able to change the id element anywhere in the document.

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" indent="yes" encoding="ISO-8859-1" version="1.0" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="id">
        <customerId>
            <xsl:apply-templates select="@*|node()"/>
        </customerId>
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

2 Comments

Ya it is working fine.Thank u so much.But when i use <xsl:template match="/"> instead of <xsl:template match="myRequest">,then not working .What is the reason?
/ is used for match the top-level document node, which is the parent of myRequest. So if you then do <xsl:apply-templates select="id" /> it is assuming that id is the root element. You would need to do <xsl:apply-templates select="myRequest/id" />

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.