English 中文(简体)
问题 xslt 将 xml 转换为输出 html
原标题:Issue with xslt transforming xml to output html

我试图用 XSLT 来简单转换 XML 来生成 HTML, 但我遇到了困难, 我似乎无法找出问题所在。 以下是我所合作的 XML 样本 :

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="C:UserscgubataDocumentsDigital Measuresjcamp_fac_ex_xslt.xsl"?>
<Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2012-02-27">
<Record userId="310106" username="jcamp" termId="453" dmd:surveyId="1154523">
    <dmd:IndexEntry indexKey="COLLEGE" entryKey="School of Business" text="School of Business"/>
    <dmd:IndexEntry indexKey="DEPARTMENT" entryKey="Accountancy" text="Accountancy"/>
    <dmd:IndexEntry indexKey="DEPARTMENT" entryKey="MBA" text="MBA"/>
    <PCI id="11454808064" dmd:lastModified="2012-02-08T13:17:39">
        <PREFIX>Dr.</PREFIX>
        <FNAME>Julia</FNAME>
        <PFNAME/>
        <MNAME>M.</MNAME>
        <LNAME>Camp</LNAME>
        <SUFFIX/>
        <ALT_NAME>Julia M. Brennan</ALT_NAME>
        <ENDPOS/>

我只想用 HTML 显示一些节点的值。 例如, 我可能想要 PREFIC、 FNAME、 LNAME 节点作为“ Julia Camp 医生” 显示( 没有引用 - 我稍后再打字 ) 。 下面是我要使用的 XSL :

<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="jcamp_fac_ex.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata"> 
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">

<xsl:value-of select="/Data/Record/PCI/PREFIX"/>

</xsl:template>
</xsl:stylesheet>

从我的研究中,这应该显示 PREFIX 字段的价值。但相反,它正在从所有节点中输出所有值(如果有4000个带有文本值的节点,我就会得到 HTML 中返回的4000个值 ) 。 我的目标是从某些节点中提取值,我可能会在表格中安排这些值。

如何从特定的节点中提取值?

最佳回答

我无法复制您的症状。 当我测试您张贴的内容时, 它根本无法产生任何输出。 因为您的 xpath 测试错误的命名空间, 这看起来是正确的 。 您需要在 xslt 中添加 < code> http:// www. digitalmeases. com/ schema/ data 命名空间的名称, 然后在 < code> value- of xpath 中使用它。 例如 :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata"
  xmlns:dm="http://www.digitalmeasures.com/schema/data">
    <xsl:output method="html" encoding="utf-8"/>
    <xsl:template match="/">
        <xsl:value-of select="/dm:Data/dm:Record/dm:PCI/dm:PREFIX"/>
    </xsl:template>
</xsl:stylesheet>
问题回答

我恐怕你已经陷入了初学者的 XSLT 头号陷阱: 我们在这个论坛上每天至少看到一次这个问题。 您的元素在命名空间中, 您的样式表正试图匹配无命名空间的节点 。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签