2014-11-19 8 views
1

КомандаXSL Трансформация для нескольких дочерних элементов

Я абсолютный новичок в 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 Input

<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.

Спасибо.

Update

Ваши ответы дал мне представление в выработках в 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"/>, чтобы получить желаемый результат ниже.

My New Желаемая Выход

<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> 
+0

Ваш вклад не содержит 'сл: label', поэтому ваш второй вопрос не может быть дан ответ. – Tomalak

+0

@Premlal, вход нечеткий относительно требуемого выхода. Можете ли вы публиковать или редактировать ввод? –

+0

Готово. Вы можете мне помочь? –

ответ

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> 
+0

Спасибо, Mate. Я обновил свой пост, чтобы включить еще один вопрос. Любая помощь будет оценена. –

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> 
+0

Спасибо, Мартин. Я обновил свой пост, чтобы включить еще один вопрос. Любая помощь будет оценена. –

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> 
+0

Спасибо. Похоже, XLST состоит в написании меньшего количества строк текста, что делает его доступным для чтения. –

+0

Я все еще жду решения для моего обновленного вопроса. Мне нужен способ включить номер главы в мой вывод. Благодарю. –

+0

Измените код, указанный выше. Вчера я сам разместил это. –

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>