XSLT使用变量与for-each选择

时间:2022-02-04 14:27:42

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和XSLT的新手,刚开始工作,我的一个项目需要我使用它。我正在尝试使用动态变量(稍后将对此变量进行硬编码)以获取某个模块的属性。这是我的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.

2 个解决方案

#1


2  

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


1  

You want <xsl:for-each select="Root/Row[Module_Name=$name]"> instead of <xsl:for-each select="Root/Row[Module_Name='$name']">. Your version compares the Module_Name element with the string literal containing the dollar symbol followed by name.

您希望 而不是 。您的版本将Module_Name元素与包含美元符号后跟name的字符串文字进行比较。

#1


2  

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


1  

You want <xsl:for-each select="Root/Row[Module_Name=$name]"> instead of <xsl:for-each select="Root/Row[Module_Name='$name']">. Your version compares the Module_Name element with the string literal containing the dollar symbol followed by name.

您希望 而不是 。您的版本将Module_Name元素与包含美元符号后跟name的字符串文字进行比较。