4

我对 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>

示例 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>

现在输出是空白的。我在想我不能以这种方式使用变量,我希望有人能以正确的方式指导我。

谢谢你。

4

2 回答 2

2

输入:

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

输出:

<?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>
于 2013-11-09T18:07:47.490 回答
1

你想要<xsl:for-each select="Root/Row[Module_Name=$name]">而不是<xsl:for-each select="Root/Row[Module_Name='$name']">. 您的版本将Module_Name元素与包含美元符号的字符串文字进行比较,后跟name.

于 2013-11-08T14:38:29.750 回答