搜索xml中的所有<tag>元素以查找其中的特定值</tag>

时间:2011-04-01 08:23:14

标签: xslt

我的问题如下:

我正在开发一个XSL,它从不同的XML文件中获取信息,以完成任务。

我有一个值,我想看看其中一个XML文件中的<Tag>元素是否保持相同的值。

我尝试使用for-each,在那里我浏览XML文件的所有<Tag>元素,并比较它们的值是否等于我要查找的值,但是当我调用它时模板,它不起作用!如果我把它从模板中取出,它至少会进入搜索循环。

有人可以帮忙吗?

我试图从中检索信息的XML文件大致如下所示:

<term-abbreviations>
  <term name=".NET">
    <abbreviation>The .NET framework is part of Windows and provides 
     a controlled environment for developing and running applications.
     Programmers do not have to "reinvent the wheel" as the framework
     provides a rich library of APIs that applications can use.</abbreviation>
  </term> 
</term-abbreviations>

我写的xsl看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
                xmlns:v="urn:schemas-microsoft-com:vml"
                xmlns:w10="urn:schemas-microsoft-com:office:word"
                xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
                xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
                xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
                xmlns:o="urn:schemas-microsoft-com:office:office"
                xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
                xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"
                xmlns:st1="urn:schemas-microsoft-com:office:smarttags"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt">


  <!-- TEMPLATE: ***GENERATE_NEWTERM_TABLE -->
  <xsl:template name="Generate_newterm_table">

    <xsl:for-each select="/document-properties/source-documents/*">      
      <xsl:for-each select="document(@name)/descendant::newTerm">

         <xsl:call-template name="Generate_newterm_table_internal">
          <xsl:with-param name="CurrentNewTerm" select="."/>          
        </xsl:call-template>   

      </xsl:for-each>
    </xsl:for-each>

    <!--<xsl:variable name="FilePath" select="/document-properties/abbreviation-document/document/@name"/>
    <xsl:for-each select="document($FilePath)/descendant::term">
      <xsl:choose>
        <xsl:when test="(.=./@name)">
          --><!--<xsl:when test="($TermToSearch=./@name)">--><!--
          <w:p wsp:rsidR="00000000" wsp:rsidRDefault="00C06CD8">
            <w:pPr>
              <w:spacing w:before="60" w:after="60" />
              <w:rPr>
                <w:b />
              </w:rPr>
            </w:pPr>
            <w:r>
              <w:rPr>
              </w:rPr>
              <w:t>
                <xsl:value-of select="concat(./@name, ' : found match')"/>
                <xsl:value-of select="./abbreviation"/>
              </w:t>
            </w:r>
          </w:p>
        </xsl:when>
        <xsl:otherwise>
          <w:p wsp:rsidR="00000000" wsp:rsidRDefault="00C06CD8">
            <w:pPr>
              <w:spacing w:before="60" w:after="60" />
              <w:rPr>
                <w:b />
              </w:rPr>
            </w:pPr>
            <w:r>
              <w:rPr>
              </w:rPr>
              <w:t>
                <xsl:value-of select="string('abbrv unavailable')"/>
              </w:t>
            </w:r>
          </w:p>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>-->

  </xsl:template>
  <!-- END TEMPLATE: ***GENERATE_NEWTERM_TABLE -->

  <!-- TEMPLATE: GENERATE_NEWTERM_TABLE_INTERNAL -->
  <xsl:template name="Generate_newterm_table_internal">
    <xsl:param name="CurrentNewTerm"/>
    <w:tr wsp:rsidR="00000000">
      <w:tblPrEx>
        <w:tblCellMar>
          <w:top w:w="0" w:type="dxa" />
          <w:bottom w:w="0" w:type="dxa" />
        </w:tblCellMar>
      </w:tblPrEx>
      <w:tc>
        <w:tcPr>
          <w:tcW w:w="2480" w:type="dxa" />
        </w:tcPr>
        <w:p wsp:rsidR="00000000" wsp:rsidRDefault="00C06CD8">
          <w:pPr>
            <w:spacing w:before="60" w:after="60" />
            <w:rPr>
              <w:b />
            </w:rPr>
          </w:pPr>
          <w:r>
            <w:rPr>
            </w:rPr>
            <w:t>
              <xsl:value-of select="$CurrentNewTerm"/>
            </w:t>
          </w:r>
        </w:p>
      </w:tc>

      <w:tc>
        <w:tcPr>
          <w:tcW w:w="2480" w:type="dxa" />
        </w:tcPr>

          <xsl:call-template name="FindAbbreviation">
            <xsl:with-param name="TermToSearch" select="$CurrentNewTerm"/>
          </xsl:call-template>

        </w:tc>
    </w:tr>     
  </xsl:template>
  <!-- END TEMPLATE: GENERATE_NEWTERM_TABLE_INTERNAL -->

  <!-- TEMPLATE: *** FIND ABBREVIATION *** -->
  <xsl:template name="FindAbbreviation">
    <xsl:param name="TermToSearch"/>

    <!--<w:p wsp:rsidR="00000000" wsp:rsidRDefault="00C06CD8">
      <w:pPr>
        <w:spacing w:before="60" w:after="60" />
        <w:rPr>
          <w:b />
        </w:rPr>
      </w:pPr>
      <w:r>
        <w:rPr>
        </w:rPr>
        <w:t>
          <xsl:value-of select="concat($TermToSearch, ' says', ' HI')"/>          
        </w:t>
      </w:r>
    </w:p>-->

<xsl:variable name="FilePath" select="/document-properties/abbreviation-document/document/@name"/>
    <xsl:for-each select="document($FilePath)/descendant::term">
      <xsl:choose>
        <xsl:when test="($TermToSearch=./@name)">
          <w:p wsp:rsidR="00000000" wsp:rsidRDefault="00C06CD8">
            <w:pPr>
              <w:spacing w:before="60" w:after="60" />
              <w:rPr>
                <w:b />
              </w:rPr>
            </w:pPr>
            <w:r>
              <w:rPr>
              </w:rPr>
              <w:t><xsl:value-of select="concat(./@name, ' : found match')"/>
                <xsl:value-of select="./abbreviation"/></w:t>
            </w:r>
          </w:p>
        </xsl:when>
        <xsl:otherwise>
          <w:p wsp:rsidR="00000000" wsp:rsidRDefault="00C06CD8">
            <w:pPr>
              <w:spacing w:before="60" w:after="60" />
              <w:rPr>
                <w:b />
              </w:rPr>
            </w:pPr>
            <w:r>
              <w:rPr>
              </w:rPr>
              <w:t><xsl:value-of select="string('abbrv unavailable')"/></w:t>
            </w:r>
          </w:p>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>

  </xsl:template>
  <!-- END TEMPLATE: *** FIND ABBREVIATION *** -->

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

<xsl:template name="FindAbbreviation">中,您选择/document-properties/abbreviation-document/document/@name,但所有XPATH表达式似乎都是<document>的相对。

$FilePath的XPATH选择更改为:

<xsl:variable name="FilePath" 
  select="/document-properties/abbreviation-document/document[@name]"/>

这将选择具有<document>的{​​{1}}元素。