Home > Enterprise >  Add comments to XML based on attribute value using xslt
Add comments to XML based on attribute value using xslt

Time:10-28

I am new to xpath and xsl and have a question. How can I add comments to the beginning and ending of each node on the XML based on an attribute of the node? For example in the below input the id:num is my attribute with value A. Any nodes in my xml can have the attribute id:num with value as A and in front of every node with the attribute id:num="A" i need to add comment as shown in the expected output section. Please help!

Example input xml:

<rootNode id:num="A">
</child nodes>
</rootNode>

Expected Output:

<!--comment--><rootNode id:num="A">
</child nodes>
</rootNode>
<!--comment--> 

I am confused as how to implement the same as I'm unsure how to check the attribute value on the xsl:template match expression

CodePudding user response:

how to check the attribute value on the xsl:template match expression

You can use a predicate:

<xsl:template match="rootNode[@id:num='A']">

(This is the only part of your question I understood. There may be other ways to solve the underlying problem.)

  • Related