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

发布于 2024-10-30 04:30:49 字数 7558 浏览 1 评论 0原文

我的问题如下:

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

我有一个值,我想查看其中一个 XML 文件中的 元素是否具有相同的值。

我尝试使用 for-each 进行操作,其中我遍历 XML 文件的所有 元素,并比较它们的值是否等于我的值正在寻找,但是当我调用模板时,它不起作用!如果我将其从模板中取出,它至少会进入搜索循环。

有人可以帮忙吗?

我尝试从中检索信息的 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>

My problem is as follows:

I am working on a XSL that takes it's info from different XML files, to get the task done.

I have a value and I want to see if the <Tag> elements in one of the XML files holds the same value.

I tried by doing with for-each, where I go through all the <Tag> elements of the XML file and compare if their value equals to the value that I am looking for, but when I call the template, it doesn't work! If I take it out of the template, it at least goes inside the search loop.

Can anyone help please?

The XML file from which I am trying to retrieve the info, looks approximately as below:

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

and the xsl I wrote looks like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2024-11-06 04:30:49

中,您选择了 /document-properties/abbreviation-document/document/@name,但是您的所有XPATH 表达式似乎期望与 相关。

$FilePath 的 XPATH 选择更改为:

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

这将选择具有 @name 元素>。

In your <xsl:template name="FindAbbreviation">, you are selecting /document-properties/abbreviation-document/document/@name, but all of your XPATH expressions seem to expect to be relative from <document>.

Change the XPATH select for $FilePath to:

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

That will select the <document> elements that have an @name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文