0

I am continuing to have a problem with the following 2 files:

Here is my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="try.xsl" ?>
<Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2017-03-08">
    <Record userId="736234" username="sample" termId="1281" dmd:surveyId="5792614">
            <dmd:IndexEntry indexKey="COLLEGE" entryKey="College Name" text="College Name"/>
            <PCI id="27109339736" dmd:lastModified="2016-12-28T17:50:26">
                    <PREFIX>Mr.</PREFIX>
                    <FNAME>John</FNAME>
                    <PFNAME>John</PFNAME>
                    <MNAME/>
                    <LNAME>Smith</LNAME>
                    <SUFFIX>Jr</SUFFIX>
                    <ALT_NAME>J</ALT_NAME>
                    <ENDPOS/>
                    <EMAIL>sample@domain.com</EMAIL>
                    <BUILDING>Central</BUILDING>
                    <ROOMNUM>100</ROOMNUM>
                    <OPHONE1>303</OPHONE1>
                    <OPHONE2>555</OPHONE2>
                    <OPHONE3>1212</OPHONE3>
                    <DPHONE1>303</DPHONE1>
                    <DPHONE2>555</DPHONE2>
                    <DPHONE3>1213</DPHONE3>
                    <FAX1/>
                    <FAX2/>
                    <FAX3/>
                    <WEBSITE/>
                    <DTM_DOB/>
                    <DTD_DOB/>
                    <DTY_DOB/>
                    <DOB_START></DOB_START>
                    <DOB_END></DOB_END>
                    <BIO/>
                    <TEACHING_INTERESTS/>
                    <RESEARCH_INTERESTS/>
            </PCI>
    </Record>
</Data>

And here is my XSL file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dta="http://www.digitalmeasures.com/schema/data"
xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata"
exclude-result-prefixes="dta dmd">-->
<xsl:output method="html" />


  <xsl:template match="node()|@*">         <!-- identity template -->
    <xsl:copy>
     <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Data">             <!-- matches /Data root element -->
    <html>
      <body bgcolor="lightgreen">
        <xsl:apply-templates select="node()|@*" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Record/PCI">      <!-- matches sub-elements -->
    <h1>Digital Measures PCI Screen Info:</h1>
    <B>Prefix: </B><xsl:value-of select="PREFIX"/><br />
    <B>Prefix: </B><xsl:value-of select="PREFIX"/><br />
    <B>First Name: </B><xsl:value-of select="FNAME"/><br />
    <B>Preferred First Name: </B><xsl:value-of select="FNAME"/><br />
    <B>Middle Name: </B><xsl:value-of select="MNAME"/><br />
  </xsl:template>

</xsl:stylesheet>

I am expecting the output to display each item in the Person Information on it's own line per the XSLT file, but instead my output is:

Mr. John John Smith Jr J sample@domain.com Central 100 303 555 1212 303 555 1213

The learning materials I used as an example are at:

https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

But I am not seeing what I am doing wrong in my case.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • _"I am expecting the output to display each item in the Person Information on it's own line "_ -- no, you are expecting the XSLT to output HTML that is _rendered_ to show each item on its own line. To eliminate the browser from the equation, run the transform locally, capture the HTML output, and show us THAT output, not what the browser does. – Jim Garrison Mar 15 '17 at 19:53
  • It's a namespace problem, and also the identity template is unnecessary and should be removed. In XPATH an unprefixed match is always taken to be in the blank namespace even if there's an `xmlns=...` in the stylesheet. – Jim Garrison Mar 15 '17 at 20:05
  • Just search for "XSLT default namespace" to find the daily answer to this daily question. (And by the way, for learning XSLT coding tips, you could do worse than spend a bit of time browsing this site.) – Michael Kay Mar 15 '17 at 20:37

3 Answers3

0

You have (correctly) declared a dta prefix and bound it to the input XML default namespace - but you're not using it. As a result, your second and third template are not matching anything, and the entire input is handled by the first template producing an output that is identical to the input.

Try instead:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dta="http://www.digitalmeasures.com/schema/data"
xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata"
exclude-result-prefixes="dta dmd">
<xsl:output method="html" />

  <xsl:template match="dta:Data">             <!-- matches /Data root element -->
    <html>
      <body bgcolor="lightgreen">
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="dta:Record/dta:PCI">      <!-- matches sub-elements -->
    <h1>Digital Measures PCI Screen Info:</h1>
    <B>Prefix: </B><xsl:value-of select="dta:PREFIX"/><br />
    <B>Prefix: </B><xsl:value-of select="dta:PREFIX"/><br />
    <B>First Name: </B><xsl:value-of select="dta:FNAME"/><br />
    <B>Preferred First Name: </B><xsl:value-of select="dta:FNAME"/><br />
    <B>Middle Name: </B><xsl:value-of select="dta:MNAME"/><br />
  </xsl:template>

</xsl:stylesheet>

Note also that you have an unmatched --> closing comment tag in line 5.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

Your difficulties result from a namespace problem.
So refer to the namespace "http://www.digitalmeasures.com/schema/data" in your <xsl:template...> rules.

This is an example of how to do this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:dta="http://www.digitalmeasures.com/schema/data" 
xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" 
exclude-result-prefixes="dta dmd">
    <xsl:output method="html" />

    <xsl:template match="node()|@*">         <!-- identity template -->
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="dta:Data">             <!-- matches /Data root element -->
        <html>
            <body bgcolor="lightgreen">
                <xsl:apply-templates select="node()|@*" />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="dta:Record/dta:PCI">       <!-- matches sub-elements -->
        <h1>Digital Measures PCI Screen Info:</h1>
        <B>Prefix: </B>
        <xsl:value-of select="dta:PREFIX" />
        <br />
        <B>Prefix: </B>
        <xsl:value-of select="dta:PREFIX" />
        <br />
        <B>First Name: </B>
        <xsl:value-of select="dta:FNAME" />
        <br />
        <B>Preferred First Name: </B>
        <xsl:value-of select="dta:FNAME" />
        <br />
        <B>Middle Name: </B>
        <xsl:value-of select="dta:MNAME" />
        <br />
    </xsl:template>

</xsl:stylesheet>
zx485
  • 28,498
  • 28
  • 50
  • 59
0

The first thing to correct: Remove --> from the end of stylesheet tag.

The second thing: Add missing namespaces. The template matching Data should be:

<xsl:template match="dta:Data">
  <html>
    <body bgcolor="lightgreen">
      <xsl:apply-templates select="dta:Record/dta:PCI" />
    </body>
  </html>
</xsl:template>

Note that I also changed the select attribute in apply-templates. This change provides that you "execute" Record/PCI right away, omitting execution of Record (with its attributes) and IndexEntry. Both of them are not HTML tags.

And the last thing: The last your template should start with <xsl:template match="dta:PCI"> (you missed namespace here too). Add also dta: in all select attributes.

So the whole XSLT should be like below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dta="http://www.digitalmeasures.com/schema/data"
  xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata"
  exclude-result-prefixes="dta dmd">
<xsl:output method="html" />

  <xsl:template match="node()|@*">
    <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
  </xsl:template>

  <xsl:template match="dta:Data">
    <html>
      <body bgcolor="lightgreen">
        <xsl:apply-templates select="dta:Record/dta:PCI" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="dta:PCI">
    <h1>Digital Measures PCI Screen Info:</h1>
    <B>Prefix: </B><xsl:value-of select="dta:PREFIX"/><br />
    <B>Prefix: </B><xsl:value-of select="dta:PREFIX"/><br />
    <B>First Name: </B><xsl:value-of select="dta:FNAME"/><br />
    <B>Preferred First Name: </B><xsl:value-of select="dta:FNAME"/><br />
    <B>Middle Name: </B><xsl:value-of select="dta:MNAME"/><br />
  </xsl:template>

</xsl:stylesheet>

I checked it using online XSLT verifier (xsltransform.net) using Xalan engine.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41