【问题标题】:XSLT - 'a href' within an element that is not part of atributeXSLT - 不属于属性的元素中的“a href”
【发布时间】:2016-02-12 06:29:16
【问题描述】:

所以我有这个我正在课堂上处理的 XML 文档。

<etymology>The <a href="https://en.wikipedia.org/wiki/Yakama">Yakama</a> Native American tribe</etymology>

它在词源元素中包含一个 href。默认情况下,当 XSLT 对此进行转换时,链接会以仅显示文本且不再链接到另一个页面的方式“停用”。

我需要知道在这里做什么:

<td width="25%">
    <xsl:value-of select="etymology"/>
</td>

这样,在 Etymology 元素中可能存在的链接将被翻译或显示为链接,而不是转换为纯文本。

我在样式表中定义的当前和唯一的模板是这样的:

<xsl:template match="/">

    <html>
        <head>
        <title>Washington Counties</title>          
        </head>

        <body>
        <h1>Washington Counties</h1>
        <p>
            ( <a><xsl:attribute name="href">
                <xsl:value-of select="counties/@source" />
            </xsl:attribute>
            data source</a> )
        </p>

        <table border="1" style="border-collapse: collapse;">

            <tr><th>County</th><th>INCITS</th><th>County Seat</th><th>Established</th><th>Origin</th><th>Etymology</th><th>Population</th><th>Area</th><th>Map</th>
            </tr>
            <xsl:for-each select="counties/county">
                <xsl:sort select="established" order="ascending" data-type="number" />
                <tr>
                <td>
                    <xsl:choose>
                        <xsl:when test="name = 'Grays Harbor County'" >
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" /> 
                            </a> 
                             <sup><a href="#b">b</a></sup>
                        </xsl:when>

                        <xsl:when test="name = 'Mason County'" >
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" /> 
                            </a> 
                             <sup><a href="#c">c</a></sup>
                        </xsl:when>

                        <xsl:when test="name = 'Kitsap County'" >
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" /> 
                            </a> 
                             <sup><a href="#d">d</a></sup>
                        </xsl:when>

                        <xsl:when test="name = 'Clark County'" >
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" /> 
                            </a> 
                             <sup><a href="#e">e</a></sup>
                        </xsl:when>

                        <xsl:when test="name = 'Yakima County'" >
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" /> 
                            </a> 
                             <sup><a href="#f">f</a></sup>
                        </xsl:when>

                        <xsl:when test="name = 'Jefferson County'" >
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" /> 
                            </a> 
                             <sup><a href="#g">g</a></sup>
                        </xsl:when>

                        <xsl:when test="name = 'Clallam County'" >
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" /> 
                            </a> 
                             <sup><a href="#g">g</a></sup>
                        </xsl:when>

                        <xsl:otherwise>
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="@href" />
                            </xsl:attribute>
                            <xsl:value-of select="name" />
                            </a>
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
                <td>
                    <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="incits/@href" />
                    </xsl:attribute>
                    <xsl:value-of select="incits" /></a>
                </td>
                <td>
                    <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="countySeat/@href" />
                    </xsl:attribute>
                    <xsl:value-of select="countySeat" /></a>
                </td>
                <td>
                    <xsl:choose>
                        <xsl:when test="established = 1879">    
                            <xsl:value-of select="established" /> <sup>
                                <a href="#a">a</a>
                            </sup>
                        </xsl:when>

                        <xsl:otherwise>
                            <xsl:value-of select="established" />
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
                <td>
                <xsl:choose>
                    <xsl:when test="origin != 0">
                        <xsl:value-of select="origin" />
                    </xsl:when>
                    <xsl:otherwise>
                        Original County
                    </xsl:otherwise>
                </xsl:choose>
                </td>
                <td width="25%">
                    <xsl:value-of select="etymology"/>
                </td>
                <td><xsl:value-of select="population" /></td>
                <td><xsl:value-of select="area[@unit='mi2']" /> sq mi
                <br />(<xsl:value-of select="area[@unit='km2']" /> sq km)
                </td>
                <td>
                <img>
                    <xsl:attribute name="src">
                        <xsl:value-of select="map/@src" />
                    </xsl:attribute>
                    <xsl:attribute name="alt">
                        <xsl:value-of select="./name" />
                    </xsl:attribute>
                </img>
                </td>
                </tr>
            </xsl:for-each>

        </table>

        <br />
        <strong>Footnotes</strong>
        <br />
        <xsl:for-each select="counties/footnote">
            <p>
            <xsl:attribute name="id">
                <xsl:value-of select="@id" />
            </xsl:attribute>
            <xsl:value-of select="@id" />. <xsl:value-of select="."/></p>
        </xsl:for-each>
        </body>     
    </html> 
</xsl:template>

最后是我正在转换的 XML 文件: http://puu.sh/n4XiV.xml

【问题讨论】:

  • 这是一个很好的例子,为什么您需要发布 minimal reproducible example 而不是脱离上下文的 sn-ps。
  • 请在您的问题中发布您的 XML(最好是最小化)。
  • 我无法发布 XML 文件,因为它超过了 30000 个字符的限制。
  • 您显然没有阅读上面的链接。具体来说,“Minimal – 使用尽可能少的代码,但仍会产生同样的问题”部分。

标签: html xml xslt


【解决方案1】:

xsl:value-of 顾名思义,只会获取节点的值。您需要使用xsl:apply-templates 让处理器继续按原样处理内容。

由于默认操作是复制,因此您将在输出中获取标记内的所有内容。

【讨论】:

    【解决方案2】:

    代替:

    <xsl:value-of select="etymology"/>
    

    做:

    <xsl:apply-templates select="etymology/node()"/>
    

    并确保您有一个复制a 和文本节点的模板。

    【讨论】:

    • 通过模板,你的意思是我必须创建一个新的
    • @Invert_Tails 如果不知道您已经拥有哪些其他模板,我无法回答您的问题。这就是我对你的问题的评论。
    • 我已经用与 sn-ps 相关的文件和模板更新了我的问题,希望对您有所帮助。
    猜你喜欢
    • 2011-04-18
    • 2012-07-01
    • 2017-10-04
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 2011-07-04
    • 2020-12-20
    相关资源
    最近更新 更多