0

I am a newbie in xslt and i want to ask for a help in creating an .xsl file that will validate if the attribute value has the same value.

<catalog>
    <book id="bk101">aa</book>
    <book id="bk102">bb</book>
    <book id="bk103">cc</book>
    <book id="bk101">dd</book>
</catalog>

choose when the value of the '@id' is not same as the other '@id'value then it will display: aa bb cc dd other wise it will display:

your '@id' has same value of 'bk101'

1
  • 2
    Please select either XSLT 1.0 or XSLT 2.0 - not both. Also post the expected output as code, not as a description. Commented Dec 18, 2014 at 11:49

1 Answer 1

2

The following stylesheet:

XSLT 1.0

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

<xsl:key name="book-by-id" match="book" use="@id" />

<xsl:template match="/catalog">
    <xsl:copy>
        <xsl:apply-templates select="book"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="book">
    <xsl:copy>
        <xsl:copy-of select="@id"/>
        <xsl:choose>
            <xsl:when test="count(key('book-by-id', @id)) > 1">
                <xsl:text>*** DUPLICATE ID ***</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

when applied to your example input, will return:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <book id="bk101">*** DUPLICATE ID ***</book>
   <book id="bk102">bb</book>
   <book id="bk103">cc</book>
   <book id="bk101">*** DUPLICATE ID ***</book>
</catalog>
Sign up to request clarification or add additional context in comments.

Comments

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.