Reputation: 55
Consider below is my xml and i have to write xslt to produce output in two column table. The first column should contain element name with attribute value and second column should contain node value.
Ex: Program
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xml.xsl"?>
<article>
<front>
<journal-meta>
<journal-id journal-id-type="pubmed">Proc Natl Acad Sci U S A</journal-id>
<journal-id journal-id-type="publisher">PNAS</journal-id>
<issn>0027-8424</issn>
<publisher>
<publisher-name>The National Academy of Sciences</publisher-name>
</publisher>
</journal-meta>
<article-meta>
<title-group>
<article-title>The coreceptor mutation</article-title>
</title-group>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Sinma</surname>
<given-names>Army D.</given-names>
</name>
</contrib>
</contrib-group>
<p>#x002A; Present address: PMD Centers for Disease Control and Prevention, Portland, OR 97232.</p>
</fn>
<corresp id="FN151">#x2020; To whom reprint requests should be addressed. E-mail: <email>[email protected]</email>.</corresp>
<fn fn-type="com">
<p>Communicated by avatar Friedman, University of Mann, Minneapolis, MN</p>
</fn>
</author-notes>
<abstract>
<p>We explore the impact of a host genetic factor on heterosexual HIV epidemics by using a deterministic mathematical model.</p></abstract>
</article-meta>
</front>
<body>
<p>Nineteen million people have</p>
<p>To exemplify the contribution</p>
<sec>
<title>The Model</title>
<p>Because we are most concerned </italic>(<italic>t</italic>)#x005D;</p>
</sec>
</body>
</article>
XSLT code tried
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="article">
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">ELEMENT</th>
<th style="text-align:left">VALUE</th>
</tr>
<xsl:for-each select="*/*">
<xsl:for-each select="*">
<tr>
<td><xsl:value-of select="local-name()"/>=> </td>
<td><xsl:value-of select="."/></td></tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Expected output should be in below format:
Element------Value
journal-id journal-id-type="pubmed----Proc Natl Acad Sci U S A
journal-id journal-id-type="publisher"---PNAS
issn----0027-8424
.
.
p------Nineteen million people have
p----To exemplify the contribution
Please help me to get the proper code
Upvotes: 0
Views: 475
Reputation: 2714
If you want your output as only text like what you have shown in your question, you could use this.
<xsl:output method="text" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:text>Element------Value
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="concat(name(parent::*), '-----', .)"/><xsl:text>
</xsl:text>
</xsl:template>
Upvotes: 1
Reputation: 117140
Consider the following example:
XML (I am not using yours, because it is not well-formed)
<article>
<front>
<journal-meta>
<journal-id journal-id-type="pubmed">Proc Natl Acad Sci U S A</journal-id>
<journal-id journal-id-type="publisher">PNAS</journal-id>
<issn>0027-8424</issn>
<publisher>
<publisher-name>The National Academy of Sciences</publisher-name>
</publisher>
</journal-meta>
<article-meta>
<title-group>
<article-title>The coreceptor mutation</article-title>
</title-group>
<contrib-group>
<contrib contrib-type="author">
<name>
<surname>Sinma</surname>
<given-names>Army D.</given-names>
</name>
</contrib>
</contrib-group>
<p>#x002A; Present address: PMD Centers for Disease Control and Prevention, Portland, OR 97232.</p>
<corresp id="FN151">#x2020; To whom reprint requests should be addressed. E-mail: <email>[email protected]</email>.</corresp>
<fn fn-type="com">
<p>Communicated by avatar Friedman, University of Mann, Minneapolis, MN</p>
</fn>
<abstract>
<p>We explore the impact of a host genetic factor on heterosexual HIV epidemics by using a deterministic mathematical model.</p>
</abstract>
</article-meta>
</front>
<body>
<p>Nineteen million people have</p>
<p>To exemplify the contribution</p>
</body>
</article>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<table border="1">
<tr>
<th>ELEMENT</th>
<th>VALUE</th>
</tr>
<xsl:for-each select="//text()">
<tr>
<td>
<xsl:value-of select="name(..)"/>
<xsl:variable name="attr" select="../@*" />
<xsl:if test="$attr">
<xsl:text>[</xsl:text>
<xsl:value-of select="name($attr)"/>
<xsl:text>=</xsl:text>
<xsl:value-of select="$attr"/>
<xsl:text>]</xsl:text>
</xsl:if>
</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Result (rendered)
Upvotes: 0