如何转换节点中的所有相同元素

时间:2018-07-01 09:05:36

标签: xslt xslt-1.0 xslt-2.0

在下面的xml文件中包含许多作者元素。我在xslt中提到过,只显示第一个元素。我想展示所有作者元素。请为未在文本中显示的元素提供xslt编码,将在浏览器中显示。

XML代码:

<?xml version="1.0" encoding="US-ASCII" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="view.xsl"?>
<!DOCTYPE WileyML3G [
<!ENTITY % wileyml3g.ent SYSTEM "http://v.wiley.com:3535/dtds/wileyml3g/wiley.ent">
%wileyml3g.ent;
]>
<bibliography xml:id="aic16349-bibl-0001" style="numbered" cited="no">
<title type="main">REFERENCE<!--<QUERY xml:id="Q2"><p>References "3&#x2013;35" were not cited anywhere in the text. Please provide a citation. Alternatively, delete the items from the list.</p></QUERY>--></title>
<bib xml:id="aic16349-bib-0001">
<citation type="journal" xml:id="aic16349-cit-0001"><!--<QUERY xml:id="Q3"><p>Reference "1"  is not cited in the text. Please indicate where it should be cited; or delete from the reference list.</p></QUERY>-->
<author><familyName>Deer</familyName> <givenNames>TR</givenNames></author>, <author><familyName>Provenzano</familyName> <givenNames>DA</givenNames></author>, <author><familyName>Hanes</familyName> <givenNames>M</givenNames></author>, et al. <articleTitle>The Neurostimulation Appropriateness Consensus Committee (NACC) Recommendations for Infection Prevention and Management</articleTitle>. <journalTitle>Neuromodulation.</journalTitle> <pubYear year="2017">2017</pubYear>;<vol>20</vol>(<issue>1</issue>):<pageFirst>31</pageFirst>&#x2010;<pageLast>50</pageLast>.</citation>
</bib>

XSLT代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>HTML VIEW</h1>
<table border="1" cellpadding="10px">
<tr>
<th>Authors</th>
<th>Year</th>
<th>Article_Title</th>
<th>Journal_Title</th>
<th>Volume</th>
<th>Issue</th>
<th>First_Page</th>
<th>Last_Page</th>
</tr>
<xsl:for-each select="bibliography/bib/citation">
<tr>
<td><span style="background-color:skyblue;"><xsl:value-of select="author"/></span>, </td>
<td><xsl:value-of select="pubYear"/></td>
<td width="75%"><xsl:value-of select="articleTitle"/></td>
<td width="25%"><xsl:value-of select="journalTitle"/></td>
<td><xsl:value-of select="vol"/></td>
<td><xsl:value-of select="issue"/></td>
<td><xsl:value-of select="pageFirst"/></td>
<td><xsl:value-of select="pageLast"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:1)

您已将问题标记为XSLT 2.0,如果您确实使用XSLT 2.0处理器并在样式表中使用version="2.0",则<xsl:value-of select="author"/>将输出所有选定的author子元素,您可以甚至使用<xsl:value-of select="author" separator=", "/>将不同的作者用,隔开。如果您使用的是XSLT 1.0,则可以使用<xsl:appy-templates select="author"/><xsl:template match="author"><xsl:if test="position() > 1">, </xsl:if></xsl:value-of select="."/></xsl:template>,如果愿意,可以使用xsl:for-each

答案 1 :(得分:0)

作为@Martin的建议,如果您真的要使用1.0,则必须与for-each作者一起使用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h1>HTML VIEW</h1>
                <table border="1" cellpadding="10px">
                    <tr>
                        <th>Authors</th>
                        <th>Year</th>
                        <th>Article_Title</th>
                        <th>Journal_Title</th>
                        <th>Volume</th>
                        <th>Issue</th>
                        <th>First_Page</th>
                        <th>Last_Page</th>
                    </tr>
                    <xsl:for-each select="bibliography/bib/citation">
                        <tr>
                            <td>
                                <span style="background-color:skyblue;">
                                    <xsl:for-each select="author">
                                        <xsl:value-of select="."/>
                                        <xsl:if test="following-sibling::author"><xsl:text>, </xsl:text></xsl:if>
                                    </xsl:for-each>
                                </span>
                            </td>
                            <td><xsl:value-of select="pubYear"/></td>
                            <td width="75%"><xsl:value-of select="articleTitle"/></td>
                            <td width="25%"><xsl:value-of select="journalTitle"/></td>
                            <td><xsl:value-of select="vol"/></td>
                            <td><xsl:value-of select="issue"/></td>
                            <td><xsl:value-of select="pageFirst"/></td>
                            <td><xsl:value-of select="pageLast"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

或者在使用2.0之类的工具时将非常容易完成

  1. 需要将XSLT版本从1.0更改为2.0
  2. 在作者价值中将@seperator,一起使用。

                                               

    HTML视图

                                                                  s                         年                         Article_Title                         Journal_Title                         体积                         问题                         第一页                         最后一页