0

My CategoryType.xml document contains following xml elements in large number (around 7 thousand).

Please also note that ATRIBUTE_NAME and ATTIBUTE_CODE combination is unique here.

<type>
	<Category ATRIBUTE_NAME="CDITP" ATTIBUTE_CODE="3">
		<referenceCode>1000</referenceCode>
		<referenceName>TelephoneNumber</referenceName>
	</Category>
	<Category ATRIBUTE_NAME="CDMTHDTP" ATTIBUTE_CODE="9">
		<referenceCode>1009</referenceCode>
		<referenceName>Contact Details</referenceName>
	</Category>
	-
	-
	-
 </type>

Now, I want to develop a function using XSLT which will take input as ATRIBUTE_NAME and ATTIBUTE_CODE and returns back referenceCode, referenceName

Why I need function because I need to do huge number of transformations in my transformation xslt and hence function would come as handy.

I've searched the internet and could not found the satisying answers.

Please help.

1 Answer 1

1

The function is named key and you simply need to define a key (https://www.w3.org/TR/xslt-30/#key)

<xsl:key name="cat" match="type/Category" use="ATRIBUTE_NAME, ATTIBUTE_CODE" composite="true"/>

in XSLT 3.0 and then call e.g.

key('cat', ('CDITP', '3'))/(referenceCode, referenceName)

to return the data. In XSLT 2.0 you can define a key as

<xsl:key name="cat" match="type/Category" use="concat(ATRIBUTE_NAME, '|', ATTIBUTE_CODE)"/>

and then call e.g.

key('cat', concat('CDITP', '|', '3'))/(referenceCode, referenceName)

.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Martin for replying!
I want to add one thing here is - my xml document is not the only xml. Basically, I will transforming the input request of a webservice and in that request there will be ATTIBUTE_CODE available and i would add ATRIBUTE_NAME and call above mentioned document to get values which eventually I will add to the request xml and then send it to recipient. Please advice accordingly.
Well, the key function has an optional third argument to provide the context document or node, so use that if you are dealing with various input documents in the same transformation. If that does not help then you need to edit your question and explain in more detail what you have.

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.