简体   繁体   English

如何在XSLT-1.0中连接XML标签?

[英]How to concat XML tags in XSLT-1.0?

I tried to process an XML file with this XSLT-1.0 file. 我试图使用此XSLT-1.0文件处理XML文件。
I would like to concat all of the <pr> tags in the same cell of the table. 我想在表的同一单元格中合并所有<pr>标记。

Here is my XML: 这是我的XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<cours>
    <sigle>GEN1051</sigle>
    <titre>Ingénierie et entreprises II</titre>
    <prgs>
        <pr>4108</pr>
        <pr>7643</pr>
        <pr>7833</pr>
    </prgs>
    <credits>3</credits>
</cours>

I tried it with the function xsl:choose and fn:concat , but of course, this didn't work, because that makes three empty cells instead of one cell with my 3 values of <pr> tags. 我使用xsl:choosefn:concat函数进行了尝试,但是当然,这是行不通的,因为这将使三个空单元格而不是一个带有3个<pr>标记值的单元格。

This is a part of my XSLT: 这是我的XSLT的一部分:

<xsl:for-each select="prgs/pr">
    <xsl:choose>
        <xsl:when test="pr = 1">
            <td>
                <xsl:value-of select="."/>
            </td>
            <xsl:otherwise>
                <td>
                    <xsl:value-of select='concat(pr, pr)'/>
                </td>
            </xsl:otherwise>
        </xsl:when>
    </xsl:choose>
</xsl:for-each>

Who knows a better solution to this problem? 谁知道这个问题的更好解决方案?

You can use the following XSLT-1.0 stylesheet: 您可以使用以下XSLT-1.0样式表:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

    <xsl:template match="prgs">
        <xsl:copy>
            <td>
                <xsl:for-each select="pr">
                    <xsl:value-of select="." />
                </xsl:for-each>
            </td>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Its output is: 其输出为:

<?xml version="1.0"?>
<cours>
    <sigle>GEN1051</sigle>
    <titre>Ingénierie et entreprises II</titre>
    <prgs>
        <td>410876437833</td>
    </prgs>
    <credits>3</credits>
</cours>

It concatenates the values of all three <pr> elements in one <td> element. 它将所有三个<pr>元素的值连接到一个<td>元素中。

If you want to separate your <pr> values, just put a 如果要分隔<pr>值,只需输入一个

<xsl:if test="position() != last()"><xsl:text> - </xsl:text></xsl:if>

after the <xsl:value-of select="." /> <xsl:value-of select="." /> <xsl:value-of select="." /> in the xsl:for-each loop. <xsl:value-of select="." />xsl:for-each循环。

If you want to put all three pr values in the same cell, you can do simply: 如果要将所有三个pr值放在同一单元格中,则可以简单地执行以下操作:

In XSLT 1.0: 在XSLT 1.0中:

<td>
    <xsl:for-each select="prgs/pr">
        <xsl:value-of select="." />
        <xsl:if test="position() != last()">; </xsl:if>
    </xsl:for-each>
</td>

In XSLT 2.0: 在XSLT 2.0中:

<td>
    <xsl:value-of select="prgs/pr" separator="; "/>
</td>

The output of both will be: 两者的输出将是:

<td>4108; 7643; 7833</td>

This is assuming you are in the context of cours (as your snippet indicates). 这是假设你是在上下文cours (如您的片段显示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM