Text attribute as value for another element

In the XML below, I would like to know how to get the value of the text in the case_id node as an attribute for the hidden input tag in the xsl sheet below. Is it possible?

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="data.xsl"?> <NewDataSet> <Cases> <Case> <case_id>30</case_id> ... ... </Case> </Cases> </NewDataset> <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <input type="hidden" name="case_num" value="?"/> </xsl:template> </xsl:stylesheet> 
+3
source share
5 answers

Just change XSLT to this, this assumes that you have only 1 case_id, otherwise you will need to go with a more specific pattern match and remove part of the path from the XPATH value that I used as an example.

 <input type="hidden" name="case-num"> <xsl:attribute name="value"> <xsl:value-of select="/NewDataSet/Cases/Case/case_id" /> </xsl:attribute> </input> 
+4
source

try it

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <input type="hidden" name="case_num"> <xsl:attribute name="value"> <xsl:value-of select="/NewDataSet/Cases/Case/case_id/text()"/> </xsl:attribute> </input> </xsl:template> </xsl:stylesheet> 

or you can do it like

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <input type="hidden" name="case_num" value="{/NewDataSet/Cases/Case/case_id/text()}"/> </xsl:template> 

+5
source

You need to expand your XSLT with more specific matches.

Next you should output input elements containing your case_id values ​​for each Case . I assumed that there is one case_id per Case . I tried to make XSLT explicit as I can, but you don't have to be as precise in your implementation if you don't want to be.

  <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="Case"> <xsl:element name="input"> <xsl:attribute name="type"> <xsl:text>hidden</xsl:text> </xsl:attribute> <xsl:attribute name="name"> <xsl:text>case_num</xsl:text> </xsl:attribute> <xsl:attribute name="value"> <xsl:value-of select="case_id"/> </xsl:attribute> </xsl:element> </xsl:template> 
+2
source

Just use AVT (attribute value pattern) as follows:

 <input type="hidden" name="case_num" value="{*/*/*/case_id}"/> 
+2
source

I changed it to:

 <input type="hidden" name="case-num"> <xsl:attribute name="value"> <xsl:value-of select="case_id" /> </xsl:attribute> </input> 

as in the foreach loop. Thanks guys, it worked!

0
source

All Articles