0
<block3>
    <tag>
        <name>113</name>
        <value>Nfeb</value>
    </tag>
    <tag>
        <name>108</name>
        <value>20234254321</value>
    </tag>
</block3>

Here in above xml we have two tags in block3. I don't want 108 tag, so I need to prepare a xslt in that I have to call only 113. How can i do that? Can anyone please help me!

2
  • 1
    consider at least putting some effort in writing somehow readable English phrases. Next time it will be downvoted. Commented Feb 15, 2011 at 9:17
  • 2
    You're going to find it easier to understand XSLT if you learn the right terminology. The block3 element actually contains 12 tags - 6 start tags and 6 end tags. It has two child elements, whose name, confusingly, is "tag". Commented Feb 15, 2011 at 10:17

2 Answers 2

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

    <xsl:template match="/*">
        <xsl:apply-templates select="tag[not(name = '108')]"/>
    </xsl:template>

    <xsl:template match="tag">
        <xsl:value-of select="
            concat(name, '+', value)
        "/>
    </xsl:template>
</xsl:stylesheet>

Result against your sample will be 113+Nfeb.

A personally hate for-each, but for clarity.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:for-each select="block3/tag[not(name = '108')]">
            <xsl:value-of select="concat(name, '+', value)"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

15 Comments

<xsl:for-each select="block3"> <xsl:template match="/*"> <xsl:apply-templates select="tag[not(name = '108')]"/> </xsl:template> <xsl:template match="tag"> <xsl:value-of select=" concat(name, '+', value) "/> </xsl:template> </xsl:for-each> .................. Error 1 'xsl:template' cannot be a child of the 'xsl:for-each' element.
bro i hv tried with above for each code but i not gttng 113 tag value thnx fr helping me bro
@praveen. Then you are trying against a different XML sample.
nope bro .. <xsl:for-each select="block3"> <xsl:for-each select="tag"> <xsl:for-each select="block3/tag[not(name = '108')]"> <xsl:value-of select="concat(name, '+', value)"/> </xsl:for-each> </xsl:for-each> </xsl:for-each>
@praveen. I really don't understand much of what you just said :(
|
0

I would create two templates. One matching all <tag> elements which does what you want, this will hit case 113. And then another one overriding it, matching the specific cases which you want to skip.

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

  <xsl:template match="tag">.....do stuff....</xsl:template>
  <xsl:template match="tag[name = '108']" />

</xsl:stylesheet>

2 Comments

hi nick can you xplain me properly because i m very new this xslt help me

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.