Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and understanding XML transform

Tags:

html

xml

xslt

I am trying to brush up on my XML skills and was taking a course on it. There is one where I am tasked to create a XML transform from an XML instance that contains APA and MLA citations.

This is where I am stuck, I have set up my XPaths but, not sure if they are set up properly. Also looking at the transform provided to me I am not sure where to put each value-of statement to get the correct results.

Any help and explanation would be greatly appreciated!

My Code:

<?xml version="1.0" encoding="UTF-8"?>
<bibliography xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bibliography.xsd" styleToOutput="APA and MLA">
    <article>
        <authors>
            <author>
                <lastName>Devine</lastName>
                <otherNames>Patricia G.</otherNames>
                <otherInitials>P.G.</otherInitials>
            </author>
            <author>
                <lastName>Sherman</lastName>
                <otherNames>Steven J.</otherNames>
                <otherInitials>S.J.</otherInitials>
            </author>
        </authors>
        <title>Intuitive Versus Rational Judgment and the Role of Stereotyping in the Human Condition: Kirk or Spock?</title>
        <parent type="Periodical">
            <title>Psychological Inquiry</title>
            <volume>3</volume>
            <number>2</number>
            <date>1992</date>
            <pages>153-159</pages>
        </parent>
    </article>
</bibliography>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="UTF-8" method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Bibliography Formatter</title>
      </head>
      <body>
    <h1>Output in <xsl:value-of select="bibliography/@styleToOutput"/> Style</h1>
    <xsl:for-each select="//article">
          <xsl:if test="contains(/bibliography/@styleToOutput, 'APA')">
            <h2>APA Style</h2>
            <p>
              <xsl:for-each select="/parent">
                <xsl:if test="position()!=last()">, &amp; </xsl:if>
              </xsl:for-each> (). <xsl:value-of select="/title"/><xsl:text> </xsl:text>
              <i></i>,  </p>
          </xsl:if>
          <xsl:if test="contains(/bibliography/@styleToOutput, 'MLA')">
            <h2>MLA Style</h2>
            <p>
              <xsl:for-each select="/authors/author">
                <xsl:choose>
                  <xsl:when test="position()=1">
                  </xsl:when>
                  <xsl:otherwise>, and <xsl:text> </xsl:text>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each></p>
          </xsl:if>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


Expected output enter image description here

like image 672
Zubair Amjad Avatar asked Aug 21 '26 09:08

Zubair Amjad


1 Answers

The code below doesn't do pixel-perfect render, but you should get the idea and complete it the way you need.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>Bibliography Formatter</title>
            </head>
            <body>
                <h1>Output in <xsl:value-of select="bibliography/@styleToOutput"/> Style</h1>
                <xsl:for-each select="//article">
                    <xsl:if test="contains(/bibliography/@styleToOutput, 'APA')">
                        <h2>APA Style</h2>
                        <p>
                            <xsl:call-template name="drawAuthors"/>
                            <xsl:for-each select="parent">
                                <xsl:if test="position()!=last()">, &amp; </xsl:if>
                            </xsl:for-each> (). <xsl:value-of select="title"/><xsl:text> </xsl:text>
                            <i></i>,  </p>
                    </xsl:if>
                    <xsl:if test="contains(/bibliography/@styleToOutput, 'MLA')">
                        <h2>MLA Style</h2>
                        <p>
                            <xsl:call-template name="drawAuthors"/>                            
                            <xsl:value-of select="title"/>
                        </p>
                    </xsl:if>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template name="drawAuthors">
        <xsl:for-each select="authors/author">
            <xsl:choose>
                <xsl:when test="position()=1">
                </xsl:when>
                <xsl:otherwise>, and <xsl:text> </xsl:text>
                </xsl:otherwise>
            </xsl:choose>                                
            <xsl:value-of select="lastName"/>
            <xsl:value-of select="otherInitials"/>
        </xsl:for-each>
    </xsl:template>
    
</xsl:stylesheet>
like image 122
Pavel Koryakin Avatar answered Aug 24 '26 00:08

Pavel Koryakin