Search code examples
xmlxsltxslt-1.0

XSLT using variable with for-each select


I'm very new to XML & XSLT and just started a job where one of my projects needs me to use this. I'm trying to use a dynamic variable (later on this variable won't be hardcoded) to get attributes of a certain module. Here is a condensed version of my XSL:

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

<xsl:template match="/">
    <style type="text/css">
        .details
            {
                margin:25px 25px;   
            }
    </style>

    <xsl:variable name="name" select="1234"/>

    <xsl:for-each select="Root/Row[Module_Name='$name']">
    <html>
        <div class="details">
            <pre>
            <b>Module:</b>      <xsl:value-of select="Module_Name"/><br></br>
            <b>Description:</b>         <xsl:value-of select="Description"/>

            </pre>
        </div>
    </html> 
    </xsl:for-each>     
</xsl:template>
</xsl:stylesheet>

Sample XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Row>
        <SI_NO>1</SI_NO>
        <Module_Name>1234</Module_Name>
        <Description>This is the description</Description>
    </Row>
</Root>

Right now the output is blank. I'm thinking I can't use variables this way, and I'm hoping someone can guide me in the right way.

Thank you.


Solution

  • Input:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Row>
            <SI_NO>1</SI_NO>
            <Module_Name>1234</Module_Name>
            <Description>This is the description</Description>
        </Row>
    </Root>
    

    XSL:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
       <xsl:output method="xml" indent="yes" />
       <xsl:template match="/">
          <style type="text/css">.details
                    {
                        margin:25px 25px;   
                    }</style>
          <xsl:variable name="name" select="1234" />
          <xsl:for-each select="Root/Row[Module_Name=$name]">
             <html>
                <div class="details">
                   <pre>
                      <b>Module:</b>
                      <xsl:value-of select="Module_Name" />
                      <br />
                      <b>Description:</b>
                      <xsl:value-of select="Description" />
                   </pre>
                </div>
             </html>
          </xsl:for-each>
       </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <?xml version="1.0" encoding="UTF-8"?>
    <style type="text/css">
            .details
                {
                    margin:25px 25px;   
                }
        </style>
    <html>
       <div class="details">
          <pre>
             <b>Module:</b>1234<br/>
             <b>Description:</b>This is the description</pre>
       </div>
    </html>