0

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.

1
  • Search the internet (Bing) for: "XPath default namespace" -- there should be hundreds of informative results. Commented May 25, 2012 at 1:43

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I knew I was missing something, and adding the namespace prefix to the schema did the trick. Thanks again!!
1

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.

2 Comments

Please provide some more targeted guidance so that the OP can understand where to focus.
Nice suggestion. There are some very patient people on this forum who will patiently answer this question as if it has never been asked before. I'm afraid I'm not one of them; but I still try to be helpful by providing some search terms that people can google on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.