简体   繁体   中英

Issue with xslt transforming xml to output html

I am trying to do a simple transform of XML using XSLT to generate HTML, but I'm having difficulties and I can't seem to figure out what the problem is. Here is a sample of the XML I am working with:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="C:\Users\cgubata\Documents\Digital Measures\jcamp_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/>

All I want to do is have the value for some of the nodes to be displayed in HTML. So for example, I might want the PREFIC, FNAME, LNAME nodes to display as "Dr. Julia Camp" (no quotes - I'll do styling later). Here's the XSL that I am using:

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

From what I been researching, that should show the value of that PREFIX field. But instead, it is outputting all of the values from all of the nodes (so if there are 4000 nodes with a text value, I am getting 4000 values returned in HTML). My goal will be to pull out the values from certain nodes, and I will probably arrange them in a table.

How do I pull out the values from a specific node? Thanks in advance.

Well, I can't reproduce your symptoms. When I test what you've posted it doesn't produce any output at all. Which looks correct because your xpath is testing the wrong namespace. You need to add in your xslt a namespace-prefix mapping for the http://www.digitalmeasures.com/schema/data namespace, and then use it in the value-of xpath. Like this:

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

I'm afraid you've fallen into the number one XSLT trap for beginners: we see this question at least once a day on this forum. Your elements are in a namespace and your stylesheet is trying to match nodes in no namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM