多个子元素的XSL转换

时间:2014-11-19 12:52:31

标签: xml xslt

我是XSLT的绝对新手。我正在尝试一些自动化。我希望能够使用以下XML文本代码创建以下输出:

我的XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0">
<xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <html>
        <head>
            <title>KT/Definitions List</title>
        </head>
        <body>
            <h2>KT/Definitions List</h2>
            <table border="1">
                <thead>
                    <tr>
                        <th>KT</th>
                        <th>Definition</th>
                    </tr>
                </thead>
                <tbody>
                        <xsl:apply-templates select="cl:doc//cl:key-term-entry"/>
                </tbody>
            </table>
        </body>
    </html>
</xsl:template>
<xsl:template match="cl:key-term-entry">
    <tr>
        <td><xsl:value-of select="cl:key-term"/></td>
        <td><xsl:value-of select="cl:key-term-def"/></td>
    </tr>
</xsl:template>
</xsl:stylesheet>

我的部分XML输入

<cl:key-term-entry identifier="JMTMRA930472614">
<cl:key-term identifier="DXYKHJ261631149">availability sampling</cl:key-term>
<cl:key-term-def identifier="BKPHVJ904214958">A sampling method that selects elements simply because of their ready availability and convenience. Frequently used in social work because it is usually less expensive than other methods and because other methods may not be feasible for a particular type of study or population. See also
<cl:style styles="italic">accidental sampling</cl:style>,
<cl:style styles="italic">convenience sampling</cl:style>, and
<cl:xref link-target="KRJPJV275444397" ordinal="11" pre-text="Chapter "/>.
</cl:key-term-def>
</cl:key-term-entry>

我的XSLT输出

<tr>
<td>availability sampling</td>
<td>A sampling method that selects elements simply because of their ready availability and convenience. Frequently used in social work because it is usually less expensive than other methods and because other methods may not be feasible for a particular type of study or population. See also accidental sampling, convenience sampling, and .</td>
</tr>

我想要的XSLT输出

<tr>
<td>availability sampling</td>
<td>A sampling method that selects elements simply because of their ready availability and convenience. Frequently used in social work because it is usually less expensive than other methods and because other methods may not be feasible for a particular type of study or population. See also <i>accidental sampling</i>, <i>convenience sampling</i>, and <a href="chapter11.html">Chapter 11</a>.</td>
</tr>

正如您从我的输出中看到的(不是我想要的输出),我无法理解如何在此处添加<i>标记和<a>标记。实际上,自我关闭的<cl:xef>标记(其属性值必须转换为<a>标记)在我的输出中为空。这里的任何帮助都将提升我对XSLT的了解。

感谢。

更新

您的回答让我深入了解了XSLT的工作原理。谢谢!但是,我忘了在这里提出另一个问题。我想在输出中捕获来自不同元素的信息。

我的更新输入XML

<cl:chapter identifier="GIZWOL818406804">
<cl:complex-meta>
    <cl:title identifier="FBXQBD997244986">Why Study Research?</cl:title>
    <cl:label>Chapter <cl:ordinal>1</cl:ordinal></cl:label>
</cl:complex-meta>
<cl:key-term-entry identifier="JMTMRA930472614">
    <cl:key-term identifier="DXYKHJ261631149">availability sampling</cl:key-term>
    <cl:key-term-def identifier="BKPHVJ904214958">A sampling method that selects elements simply because of their ready availability and convenience. Frequently used in social work because it is usually less expensive than other methods and because other methods may not be feasible for a particular type of study or population. See also
        <cl:style styles="italic">accidental sampling</cl:style>,
        <cl:style styles="italic">convenience sampling</cl:style>, and
    <cl:xref link-target="KRJPJV275444397" ordinal="11" pre-text="Chapter "/>.
    </cl:key-term-def>
</cl:key-term-entry>
</cl:chapter>

我更新的部分XSLT

<xsl:template match="cl:key-term-entry">
    <tr>
        <td><xsl:apply-templates select="cl:key-term"/></td>
        <td><xsl:apply-templates select="cl:key-term-def"/></td>
    </tr>
</xsl:template>
<xsl:template match="//cl:chapter/cl:complex-meta/cl:label">
    <td><xsl:value-of select="."/></td>
</xsl:template>

我不确定在哪里应该包含<xsl:apply-templates select="cl:label"/>以获得以下所需的输出。

我的新需求输出

<tr>
                <td>scientific method</td>
                <td>An approach to inquiry that attempts to safeguard against errors commonly made in casual human inquiry. Chief features include viewing all knowledge as provisional and subject to refutation, searching for evidence based on systematic and comprehensive observation, pursuing objectivity in observation, and replication. See Chapter 1.</td>
                <td>Chapter 1</td>

</tr>
<tr>...</tr>

4 个答案:

答案 0 :(得分:1)

而不是简单地使用value-of使用apply-templates

<xsl:template match="cl:key-term-entry">
    <tr>
        <td><xsl:apply-templates select="cl:key-term"/></td>
        <td><xsl:apply-templates select="cl:key-term-def"/></td>
    </tr>
</xsl:template>

当然,您需要为要转换的其他元素添加模板,例如

<xsl:template match="cl:style[styles = 'italic']">
  <i>
   <xsl:apply-templates/>
  </i>
</xsl:template>

<xsl:template match="cl:xref">
  <a href="{@pre-text}{@ordinal}.html"><xsl:value-of select="concat(@pre-text, ' ', @ordinal)"/></a>
</xsl:template>

答案 1 :(得分:0)

试试这个:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:cl="http://www.examples">
<xsl:output method="xml" version="5.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <html>
        <head>
            <title>KT/Definitions List</title>
        </head>
        <body>
            <h2>KT/Definitions List</h2>
            <table border="1">
                <thead>
                    <tr>
                        <th>KT</th>
                        <th>Definition</th>
                    </tr>
                </thead>
                <tbody>
                        <xsl:apply-templates select="//cl:key-term-entry"/>
                </tbody>
            </table>
        </body>
    </html>
</xsl:template>
<xsl:template match="cl:key-term-entry">
    <tr>
        <td><xsl:apply-templates select="cl:key-term"/></td>
        <td><xsl:apply-templates select="cl:key-term-def"/></td>
    </tr>
</xsl:template>

<xsl:template match="cl:style">
    <xsl:choose>
        <xsl:when test="@styles='italic'">
            <i><xsl:apply-templates/></i>
        </xsl:when>
        <xsl:when test="@styles='bold'">
            <b><xsl:apply-templates/></b>
        </xsl:when>
        <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="cl:xref">
    <a>
        <xsl:attribute name="href">
            <xsl:value-of select="concat(normalize-space(@pre-text), @ordinal, '.html')"/>
        </xsl:attribute>
        <xsl:value-of select="concat(@pre-text, @ordinal)"/>
    </a>
</xsl:template>


</xsl:stylesheet>

答案 2 :(得分:0)

对于其他要求,请按照以下说明进行更新。

更改

    <tr>
        <td><xsl:apply-templates select="cl:key-term"/></td>
        <td><xsl:apply-templates select="cl:key-term-def"/></td>
    </tr>

to

    <tr>
        <td><xsl:apply-templates select="cl:key-term"/></td>
        <td><xsl:apply-templates select="cl:key-term-def"/></td>
        <td><xsl:value-of select="concat(cl:key-term-def/cl:xref/@pre-text, cl:key-term-def/cl:xref/@ordinal)"/></td>
    </tr>

答案 3 :(得分:0)

最后,我得到了我正在寻找的答案。感谢@Rudramuni和@Martin:

我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:cl="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <html>
        <head>
            <title>KT List</title>
        </head>
        <body>
            <table border="1">
                <thead>
                    <tr>
                        <th>KT</th>
                        <th>Definition</th>
                        <th>Chapter Number</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:for-each select="//cl:key-term-entry">
                        <tr>
                            <td><xsl:apply-templates select="cl:key-term"/></td>
                            <td><xsl:apply-templates select="cl:key-term-def"/></td>
                            <td><xsl:apply-templates select="ancestor::cl:chapter/cl:complex-meta/cl:label"/></td>
                        </tr>
                    </xsl:for-each>
                </tbody>
            </table>            
        </body>
    </html>
</xsl:template>
<xsl:template match="cl:style">
    <xsl:choose>
        <xsl:when test="@styles='italic'">
            <i><xsl:apply-templates/></i>
        </xsl:when>
        <xsl:when test="@styles='bold'">
            <b><xsl:apply-templates/></b>
        </xsl:when>
        <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="cl:xref"><xsl:value-of select="concat(@pre-text,@ordinal)"/></xsl:template>