XSLT using a variable with every choice

I am very new to XML and XSLT and just got started when one of my projects requires me to use this. I am trying to use a dynamic variable (later this variable will not be hard-coded) to get the attributes of a specific module. Here is a compressed 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>

XML example:

<?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>

The output is currently empty. I think that I cannot use variables in this way, and I hope that someone can guide me correctly.

Thanks.

+4
source share
2 answers

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>
+2
source

<xsl:for-each select="Root/Row[Module_Name=$name]"> <xsl:for-each select="Root/Row[Module_Name='$name']">. Module_Name , , name.

+1

All Articles