tags:

views:

1223

answers:

5

With the XML below, I would like to know how to get the value of text in the case_id node as an attribute for the hidden input tag in the xsl sheet below. Is this 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"&gt;
<xsl:template match="/">
<input type="hidden" name="case_num" value="?"/>
</xsl:template>
</xsl:stylesheet>
+1  A: 

You need to expand your XSLT with some more specific matches.

The following should output input elements that contain your case_id values for each Case. I've assumed there is one case_id per Case. I've tried to make the XSLT as explicit as I can, but you don't need to be this exact 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>
Jeff Yates
Why go through all of the hassle of the big XSL items rather than like what I have listed
Mitchel Sellers
Like I said, I am being as explicit as I can. That way, those new to XSLT aren't going to wonder if "input" is an XSLT command or not. After all, this site is intended to help people out so I wanted to remove any ambiguity.
Jeff Yates
+1  A: 

Just change your XSLT to this, this assumes that you do only have 1 case_id, otherwise, you will need to go with a more specific template match, and remove some of the path in the XPATH value 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>
Mitchel Sellers
+2  A: 

Try this

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <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 inline like this

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

joegtp
A: 

I changed it to:

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

as it's in a foreach loop. Thanks guys, that worked a treat!

A: 

Just use an AVT (Attribute Value Template) like this:

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

Dimitre Novatchev