将后代标签显示为XSLT中的标签

问题描述 投票:0回答:1

我想在XSLT输出中将某些特定的后代标记显示为标记:

我有一些类似于以下内容的xml:

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='item-jerd.xsl'?>
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xml:lang="en" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" article-type="research-article">
<front id="meta">
<article-meta id="art">
<contrib-group>
<affiliation id="affiliation1">Production Engineering,<institution-wrap><institution>Engineering and Technology</institution></institution-wrap><city>Dhanga</city><country country="IN">India</country></affiliation>
<affiliation id="affiliation2">Industrial and Production Engineering,<institution-wrap><institution>India University of Engineering and Technology</institution></institution-wrap><city>Dukka</city><country country="IN">India</country></affiliation>
<affiliation id="affiliation3">Industrial Systems Engineering,<institution-wrap><institution>University of Regina</institution></institution-wrap><city>Regina</city>, Saskatchewan,<country country="CA">Canada</country></affiliation>
</contrib-group>
<author-notes>
<corresp id="cor1">GBWIN is the owner and can be contacted at:<ext-link ext-link-type="email" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="mailto:[email protected]">[email protected]</ext-link></corresp>
</author-notes>
</article-meta>
</front>
<body/>
</article>

我使用了以下xslt条件:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head>
<title>HELP ME</title>
</head>
<body onkeyup="keyDown=0" onkeydown="keyDown=1">
<div class="content" id="main">
<xsl:call-template name="main"/>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="journalStylesheet/baseData" name="main">
<h1>Author info</h1>
<table  border="1" width="1200" bgcolor="#CCFFCC">      
<xsl:for-each select="//affiliation">
<tr>
<td width="5%">
<font class="head3">
<xsl:variable name="attr" select="./@id" />
<xsl:if test="$attr">                               
<a name="{$attr}">
<xsl:value-of select="$attr"/>
</a>                                
</xsl:if>
</font>
</td>
<td width="95%">
<font class="head3">
<xsl:value-of select="."/>
</font>
</td>
</tr>
</xsl:for-each>
</table>
<h2>Author Notes</h2>
<table  border="1" width="1200" bgcolor="#CCFFCC"> 
<xsl:for-each select="//author-notes/*"><tr>
<td width="40%" class="head3">Author-notes</td>
<td width="60%" class="head3">
<xsl:value-of select="."/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

我想要下面的从属关系和作者注释标签的HTML输出

Production Engineering,<institution-wrap><institution>Engineering and Technology</institution></institution-wrap><city>Dhanga</city><country country="IN">India</country><br />
Industrial and Production Engineering,<institution-wrap><institution>India University of Engineering and Technology</institution></institution-wrap><city>Dukka</city><country country="IN">India</country><br />
Industrial Systems Engineering,<institution-wrap><institution>University of Regina</institution></institution-wrap><city>Regina</city>, Saskatchewan,<country country="CA">Canada</country>

但是将输出作为

生产工程,工程和技术DhangaIndia印度工程技术大学工业与生产工程DukkaIndia里贾纳大学里贾纳工业系统工程,萨斯喀彻温省,加拿大标签应以html显示以输出

html xml xslt
1个回答
0
投票

在XSLT-1.0中,您可以通过disable-output-escaping="yes"的(并非始终实现)xsl:value-of属性来实现。因此,请进行以下修改:

  1. 将调用模板更改为使用mode修饰符调用子模板,并添加xsl:if以添加最终的<br>标签:

    ...
    <td width="95%">
        <font class="head3">
            <xsl:apply-templates select="node()" mode="serialize" />
            <xsl:if test="position() != last()">
                <br />
            </xsl:if>
        </font>
    </td>
    ...
    
  2. 添加以下模板以在此模式下处理子元素:

    <!-- Remove leading and trailing spaces from each text() node -->
    <xsl:template match="text()" mode="serialize">
        <xsl:value-of select="normalize-space(.)" />
    </xsl:template>
    
    <!-- Skip all attributes in this mode because they are handled otherwise -->
    <xsl:template match="@*" mode="serialize" />
    
    <!-- Recreate all elements in this mode -->
    <xsl:template match="*" mode="serialize">
        <!-- ReCreate the current element tag -->
        <xsl:value-of disable-output-escaping="yes" select="concat('&lt;',name())" />
        <!-- Copy the attributes of the current element -->
        <xsl:if test="@*">
            <xsl:value-of select="' '" />
            <xsl:for-each select="@*">
                <xsl:value-of select="concat(name(),'=&quot;',.,'&quot;')" />
            </xsl:for-each>
        </xsl:if>
        <!-- Close the opening element -->
        <xsl:value-of disable-output-escaping="yes" select="'&gt;'" />
        <!-- Apply all sub-nodes -->
        <xsl:apply-templates select="node()|@*" mode="serialize" />
        <!-- ReCreate the closing element tag -->
        <xsl:value-of disable-output-escaping="yes" select="concat('&lt;/',name(),'&gt;')" />
    </xsl:template>
    

部分输出是:

...
<table border="1" width="1200" bgcolor="#CCFFCC">
  <tr>
    <td width="5%">
        <font class="head3">
            <a name="affiliation1">affiliation1</a>
        </font>
    </td>
    <td width="95%">
        <font class="head3">
            Production Engineering,
            <institution-wrap>
                <institution>Engineering and Technology</institution>
            </institution-wrap>
            <city>Dhanga</city>
            <country country="IN">India</country>
            <br>
        </font>
    </td>
</tr>
<tr>
    <td width="5%">
        <font class="head3">
            <a name="affiliation2">affiliation2</a>
        </font>
    </td>
    <td width="95%">
        <font class="head3">
                Industrial and Production Engineering,
                <institution-wrap>
                    <institution>India University of Engineering and Technology</institution>
                </institution-wrap>
                <city>Dukka</city>
                <country country="IN">India</country>
                <br>
            </font>
        </td>
    </tr>
    <tr>
        <td width="5%">
            <font class="head3">
                <a name="affiliation3">affiliation3</a>
            </font>
        </td>
        <td width="95%">
            <font class="head3">
                    Industrial Systems Engineering,
                    <institution-wrap>
                        <institution>University of Regina</institution>
                    </institution-wrap>
                    <city>Regina</city>
                    , Saskatchewan,<country country="CA">Canada</country>
            </font>
      </td>
  </tr>
</table>
...

接近您想要的。由于输出方法HTML,<br />标签显示为<br>标签。

© www.soinside.com 2019 - 2024. All rights reserved.