jexx2345
jexx2345

Reputation: 678

XSLT Match issue

I have the following XSLT question:

Suppose I have this XML

 <items>
    <item>
      <type>dog</type>
      <color>brown</color>
    </item>
    <item>
      <type>dog</type>
      <color>brown</color>
    </item>
    <item>
      <type/>
      <color>none</color>
    </item>
    <item>
      <type>dog</type>
      <color>black</color>
    </item>
 </items>

If I use the following in xsl 1.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="item[type='dog']">
        <item>
            <itemType><xsl:value-of select="type"/></itemType>
            <itemColor><xsl:value-of select="color"/></itemColor>
        </item>
    </xsl:template>

</xsl:stylesheet>

It will only show the first matches before the empty node.

Is there anything I am overlooking?

Upvotes: 0

Views: 175

Answers (1)

andyb
andyb

Reputation: 43823

Simple example works for me when I load the test.xml in IE8. I get the output dog dog dog

Save this as text.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<items>
    <item>
      <type>dog</type>
    </item>
    <item>
      <type>dog</type>
    </item>
    <item>
      <type>cat</type>
    </item>
    <item>
      <type>dog</type>
    </item>
</items>

Save this as test.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <xsl:apply-templates />
    </body>
    </html>
</xsl:template>
<xsl:template match="item"/> <!-- default item match (prints nothing) -->
<xsl:template match="item[type='dog']">
    <xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>

If this is not helping, please edit your question with more information about your problem.

Upvotes: 1

Related Questions