XSLT 使用带有 for-each 选择的变量

问题描述 投票:0回答:3

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

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

谢谢你。

xml xslt xslt-1.0
3个回答
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>

1
投票

您想要

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


0
投票

您没有在 XML 文件中定义 XSL 文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
**<?xml-stylesheet type="text/xsl" href="testing.xsl"?>**
<Root>
    <Row>
        <SI_NO>1</SI_NO>
        <Module_Name>1234</Module_Name>
        <Description>This is the description</Description>
    </Row>
</Root>
© www.soinside.com 2019 - 2024. All rights reserved.