【问题标题】:XSLT stylesheet not showing valuesXSLT 样式表不显示值
【发布时间】:2015-05-14 16:21:01
【问题描述】:

我正在尝试使用 XSLT 样式表将 XML 源转换为 HTML 页面。

XML 代码在这里:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="P_44.xsl"?>
<JournalData>
<PatientData><Patient><Name>Jones, John</Name>
<CodeNumber>191212121212</CodeNumber>
<CodeNumberType>C</CodeNumberType>
<BirthDate>1912-12-12</BirthDate>
<Sex>F</Sex>
<Deceased>0</Deceased>
<DoubleReg>0</DoubleReg>
<InterpreterNeeded>0</InterpreterNeeded>
</Patient>
</PatientData>
</JournalData>

XSLT 样式表在这里:

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

<xsl:template match="/">
<html>
<body>
<h1>Journal</h1>
<table border="1">
<tr bgcolor="#9acd32">
<th>Element name</th>
<th>Contence</th>
</tr>
<tr bgcolor="#FBF5A4">
<th>Patient</th>
</tr>
<xsl:for-each select="JournalData/PatientData/Patient/Name">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="Name" /></td>
</tr>  
</xsl:for-each>

<xsl:for-each select="JournalData/PatientData/Patient/CodeNumber">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="CodeNumber" /></td>
</tr>
</xsl:for-each>

<xsl:for-each select="JournalData/PatientData/Patient/CodeNumberType">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="CodeNumberType" /></td>
</tr>
</xsl:for-each>

<xsl:for-each select="JournalData/PatientData/Patient/BirthDate">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="BirthDate" /></td>
</tr>
</xsl:for-each>

<xsl:for-each select="JournalData/PatientData/Patient/Sex">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="Sex" /></td>
</tr>
</xsl:for-each>

<xsl:for-each select="JournalData/PatientData/Patient/Deceased">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="Deceased" /></td>
</tr>
</xsl:for-each>

<xsl:for-each select="JournalData/PatientData/Patient/DoubleReg">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="DoubleReg" /></td>
</tr>
</xsl:for-each>

<xsl:for-each select="JournalData/PatientData/Patient/InterpreterNeeded">
<tr>
<td><xsl:value-of select="local-name()" /></td>
<td><xsl:value-of select="InterpreterNeeded" /></td>
</tr>
</xsl:for-each>
</table> 
</body>
</html>
</xsl:template>

</xsl:stylesheet>

这个转换成一个漂亮的两列 HTML 表格,其中第一列打印了元素名称,但第二列是空白的。为什么不显示值?样式表有什么问题?


非常感谢你们俩。我现在开始工作了。这些列现在继续向下,黄色背景上的新标题(就像“Patient”)称为“PatientAddress”。这是进一步嵌套的一步:

变成

这一切都很好,只是为了一个烦人的细节。由于元素名称“PatientAdress”是“Patient”下的一个子元素,因此它变成了双重打印。首先,当打印“Patient”下的所有孩子(这是正确的术语吗?)时,“PatientAdress”下的所有值都排成一行,然后当我设置

如何摆脱第一个实例?看起来像这样:

PatientAddress Bondstreet 9 211 11 默西萨里 12 萨里 72

应该看起来像这样:

<tr bgcolor="#FBF5A4"><th>PatientAddress</th></tr><tr><td>Address1</td>   <td>Bondstreet 9</td></tr><tr><td>ZipCode</td><td>211 11</td></tr><tr><td>City</td><td>Mersey</td></tr><tr><td>State</td><td>Surrey</td></tr><tr><td>StateCode</td><td>12</td></tr><tr><td>District</td><td>Surrey</td></tr><tr><td>DistrictCode</td><td>72</td></tr></tbody></table></body></html>

(试图粘贴一个好看的表格,但没有成功....:-()

【问题讨论】:

  • 在过去三年中您提出的 8 个问题中,您接受了 0 个答案。接受答案表明答案对您有用,从而有助于未来的读者。它还增加了提问者和回答者的声誉,随着时间的推移反映出他们的帮助。阅读如何接受以及更多关于接受here 的信息,并请考虑接受对他的问题和您过去问题的回答。谢谢。

标签: html xml xslt


【解决方案1】:

for-each 内部,您需要简单地使用&lt;xsl:value-of select="."/&gt; 来输出上下文节点的字符串值。如果您使用&lt;xsl:value-of select="foo"/&gt;,那么您将查找该上下文节点名称的子元素。

【讨论】:

    【解决方案2】:

    只需对每个PatientPatient 的每个子对象进行一次迭代:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <html>
          <body>
            <h1>Journal</h1>
            <table border="1">
              <tr bgcolor="#9acd32">
                <th>Element name</th>
                <th>Contence</th>
              </tr>
              <xsl:for-each select="JournalData/PatientData/Patient">
                <tr bgcolor="#FBF5A4">
                  <th>Patient</th>
                </tr>
                <xsl:for-each select="*">
                  <tr>
                    <td><xsl:value-of select="local-name()"/></td>
                    <td><xsl:value-of select="." /></td>
                  </tr>  
                </xsl:for-each>
              </xsl:for-each>
            </table> 
          </body>
        </html>
      </xsl:template>
    </xsl:stylesheet>
    

    然后 XSLT 将像这样设置您的 XML 样式:

    【讨论】:

    • 非常感谢。我现在开始工作了。这些列现在继续向下,黄色背景上的新标题(就像“Patient”)称为“PatientAddress”。这是进一步的嵌套: 变为
    【解决方案3】:

    问题是在你的&lt;xsl:for-each&gt; 元素中,你已经匹配了你想要的元素。例如:

    <xsl:for-each select="JournalData/PatientData/Patient/Name">
        <tr>
            <td><xsl:value-of select="local-name()" /></td>
            <td><xsl:value-of select="Name" /></td>
        </tr>  
    </xsl:for-each>
    

    这里,您已经在&lt;xsl:for-each&gt; 中匹配了Name,所以在&lt;xsl:value-of select="Name" /&gt; 中您实际上是在尝试查找Name 的子Name 的值。而且没有。您应该简单地使用&lt;xsl:value-of select="."/&gt; 来获取您所在元素的值。

    而不是替换模板中的所有此类行,您可以按如下方式执行一次:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:template match="/">
            <html>
                <body>
                    <h1>Journal</h1>
                    <table border="1">
                        <tr bgcolor="#9acd32">
                            <th>Element name</th>
                            <th>Contence</th>
                        </tr>
                        <tr bgcolor="#FBF5A4">
                            <th>Patient</th>
                        </tr>
    
                        <xsl:apply-templates select="//Patient/*"/>
                    </table>
                </body>
            </html>
        </xsl:template>
    
        <xsl:template match="Patient/*">
            <tr>
                <td>
                    <xsl:value-of select="local-name()" />
                </td>
                <td>
                    <xsl:value-of select="." />
                </td>
            </tr>
        </xsl:template>
    
    </xsl:stylesheet>
    

    这将为您在此处显示的输入获得所需的结果,但我不知道您希望它如何查找许多患者记录。

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多