XSLT遍历以获得所需的值



我有以下文件要处理:

<root>
 <html>
  <table class=" table search-results-property-table">  
   <prefterm>Abies</prefterm>
    <tr>
     <td>
      <span class="versal property-click" title="Broader concept">BROADER CONCEPT</span>
     </td>
     <td>
      <ul>
       <li>
        <a class="versal" href="../../../agrovoc/en/page/c_5886">Pinaceae</a>
       </li>
      </ul>
     </td>
    </tr>
    <tr>
     <td>
      <span class="versal property-click" title="Narrower concepts.">NARROWER CONCEPTS</span>
     </td>
     <td>
      <ul>
       <li>
        <a class="versal" href="../../../agrovoc/en/page/c_11">Abies alba</a>
       </li>
       <li>
        <a class="versal" href="../../../agrovoc/en/page/c_26316">Abies amabilis</a>
                ....
       <li>
        <a class="versal" href="../../../agrovoc/en/page/c_26323">Abies veitchii</a>
       </li>
              ....
  </table>
 </html>
 <html>
         .... (another set to crosswalk)
 </html>
</root>

使用下面的xslt,我可以得到只有一个值的更广泛的概念的值:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
 <xsl:template match="root">
  <xsl:for-each select="html">
   <xsl:text>START HERE</xsl:text>
   <xsl:text>&#13;&#10;</xsl:text>
   <xsl:text>=LDR  00000nam  2200000Ia 4500</xsl:text>
   <xsl:text>&#13;&#10;</xsl:text>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="table/tr/td/span">
  <xsl:choose>
   <xsl:when test="@title='Broader concept'">
    <xsl:text>=301  \$a</xsl:text><xsl:value-of select="." />
    <xsl:text>$b</xsl:text>
    <xsl:value-of select="../../td/ul/li/a" />
    <xsl:text>$c</xsl:text>
    <xsl:value-of select="../../td/ul/li/a/@href" />
    <xsl:text>&#13;&#10;</xsl:text>
   </xsl:when>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

代码似乎与那些只有一个值的工作,我尝试添加xsl:for-each,但我不知道在哪里放,因为我是横向直到"a"。我尝试在第一个"xsl:choose"之前添加xsl:for-each select="//a",并在xsl:when test="@title= "较窄的概念之后添加它。’"但无济于事。我希望输出如下所示:

START HERE
=LDR  00000nam  2200000Ia 4500
=301  \$abroaderterm$bPinaceae
=302  \$anarrowerterm$bAbies alba$cc_11
=302  \$anarrowerterm$bAbies amabilis$cc_26316
   ....
=302  \$anarrowerterm$bAbies veitchii$cc_26323
START HERE
=LDR  00000nam  2200000Ia 4500
 (data set 2)
  ....

那么有人能告诉我应该做什么,或者我错过了什么,或者有其他方法来解决我的用例吗?谢谢大家,干杯!更新:请注意,我有不止一个人行横道,比如:

<root>
 <html>
     .... dataset 1
 </html>
 <html>
     .... dataset 2
 </html>
     .... more than 2 datasets
</root>

作为参考,文件可以在这里找到:http://128.199.159.143/mergedhtmltest_forprocess_span.xml

给定格式良好的XML:

<root>
    <html>
        <table class=" table search-results-property-table">  
            <prefterm>Abies</prefterm>
            <tr>
                <td>
                    <span class="versal property-click" title="Broader concept">BROADER CONCEPT</span>
                </td>
                <td>
                    <ul>
                        <li>
                            <a class="versal" href="../../../agrovoc/en/page/c_5886">Pinaceae</a>
                        </li>
                    </ul>
                </td>
            </tr>
            <tr>
                <td>
                    <span class="versal property-click" title="Narrower concepts.">NARROWER CONCEPTS</span>
                </td>
                <td>
                    <ul>
                        <li>
                            <a class="versal" href="../../../agrovoc/en/page/c_11">Abies alba</a>
                        </li>
                        <li>
                            <a class="versal" href="../../../agrovoc/en/page/c_26316">Abies amabilis</a>
                            </li>
                            <li>
                                <a class="versal" href="../../../agrovoc/en/page/c_26323">Abies veitchii</a>
                            </li>
                    </ul>
                </td>
            </tr>
        </table>
    </html>
</root>

和以下样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
    <xsl:template match="root">
        <xsl:for-each select="html">
            <xsl:if test="position() &gt; 1">
                <xsl:text>&#13;&#10;</xsl:text>
            </xsl:if>
            <xsl:text>START HERE</xsl:text>
            <xsl:text>&#13;&#10;</xsl:text>
            <xsl:text>=LDR  00000nam  2200000Ia 4500</xsl:text>
            <xsl:apply-templates select="table/tr"/>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="table/tr">
        <xsl:variable name="Concepts">
            <xsl:choose>
                <xsl:when test="td[1]/*[1][local-name()='span']">
                    <xsl:value-of select="td[1]/span/@title"/>
                </xsl:when>
                <xsl:when test="td[1]/*[1][local-name()='narrow']">
                    <xsl:value-of select="td[1]/narrow/@title"/>
                </xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:for-each select="td[2]//a">
            <xsl:choose>
                <xsl:when test="$Concepts='Broader concept'">
                    <xsl:text>&#13;&#10;</xsl:text>
                    <xsl:text>=301  \$a</xsl:text><xsl:value-of select="'broaderterm'" />
                    <xsl:text>$b</xsl:text>
                    <xsl:value-of select="." />
                    <xsl:text>$c</xsl:text>
                    <xsl:call-template name="tokenizeString">
                        <xsl:with-param name="list" select="@href"/>
                        <xsl:with-param name="delimiter" select="'/'"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="$Concepts='Narrower concepts.'">
                    <xsl:text>&#13;&#10;</xsl:text>
                    <xsl:text>=302  \$a</xsl:text><xsl:value-of select="'narrowerterm'" />
                    <xsl:text>$b</xsl:text>
                    <xsl:value-of select="." />
                    <xsl:text>$c</xsl:text>
                    <xsl:call-template name="tokenizeString">
                        <xsl:with-param name="list" select="@href"/>
                        <xsl:with-param name="delimiter" select="'/'"/>
                    </xsl:call-template>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
    <xsl:template name="tokenizeString">
        <xsl:param name="list"/>
        <xsl:param name="delimiter"/>
        <xsl:choose>
            <xsl:when test="contains($list, $delimiter)">
                <!-- do nothing, in essence deleting the values -->
                <xsl:call-template name="tokenizeString">
                    <xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$list"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

输出:

START HERE
=LDR  00000nam  2200000Ia 4500
=301  \$abroaderterm$bPinaceae$cc_5886
=302  \$anarrowerterm$bAbies alba$cc_11
=302  \$anarrowerterm$bAbies amabilis$cc_26316
=302  \$anarrowerterm$bAbies veitchii$cc_26323

相关内容

  • 没有找到相关文章

最新更新