首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用另一个XML标记的属性值中的引用查找XML标记并显示它?

如何使用另一个XML标记的属性值中的引用查找XML标记并显示它?
EN

Stack Overflow用户
提问于 2016-07-02 04:46:20
回答 1查看 379关注 0票数 0

我想为下面的XML生成一个HTML输出。但是,我不想在XML中显示所有内容,而是在特定标记中显示一些文本。

这是XML

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE article SYSTEM "DTD.dtd">
<at dtd-version="1.0" article-type="RA">
  <first>
    <a-meta>
      <a-cat>
        <sg sg-type="A Type">
          <sub>RA</sub>
        </sg>
        <sg sg-type="Cat">
          <sub>OSD</sub>
        </sg>
        <sg sg-type="Class">
          <sub>OS</sub>
          <sg>
            <sub>IT</sub>
            <sg>
              <sub>Air-SE</sub>
              <sub>Sea-IP</sub>
              <sub>BC</sub>
            </sg>
          </sg>
          <sg>
            <sub>Regions</sub>
            <sg>
              <sub>AO</sub>
            </sg>
          </sg>
          <sg>
            <sub>BC - O</sub>
          </sg>
        </sg>
      </a-cat>
      <t-group>
        <a-title>ABC</a-title>
        <alt-title alt-title-type="r-h">abc</alt-title>
      </t-group>
      <c-group>
        <ctrb ctrb-type="au" corresp="yes">
          <role content-type="1" />
          <name>
            <sname>Sname1</sname>
            <g-names>Fname1</g-names>
            <prefix>Ms.</prefix>
          </name>
          <ctrb-id ctrb-id-type="orcid" specific-use="authenticated">123-456-789</ctrb-id>
          <degrees>PhD</degrees>
          <email>email1@email.com</email>
          <xref ref-type="aff" rid="aff1" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="2" />
          <name>
            <sname>Sname2</sname>
            <g-names>Fname2</g-names>
          </name>
          <email>email2@email.com</email>
          <xref ref-type="aff" rid="aff2" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="3" />
          <name>
            <sname>Sname3</sname>
            <g-names>Fname3</g-names>
          </name>
          <email>email3@email.com</email>
          <xref ref-type="aff" rid="aff3" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="4" />
          <name>
            <sname>Sname4</sname>
            <g-names>Fname4</g-names>
          </name>
          <email>email4@email.com</email>
          <xref ref-type="aff" rid="aff4" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="5" />
          <name>
            <sname>Sname5</sname>
            <g-names>Fname5</g-names>
          </name>
          <email>email5@email.com</email>
          <xref ref-type="aff" rid="aff5" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="6" />
          <name>
            <sname>Sname6</sname>
            <g-names>Fname6</g-names>
          </name>
          <email>email6@email.com</email>
          <xref ref-type="aff" rid="aff6" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="7" />
          <name>
            <sname>Sname7</sname>
            <g-names>Fname7</g-names>
          </name>
          <email>email7@email.com</email>
          <xref ref-type="aff" rid="aff7" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="8" />
          <name>
            <sname>Sname8</sname>
            <g-names>Fname8</g-names>
          </name>
          <email>email8@email.com</email>
          <xref ref-type="aff" rid="aff8" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="9" />
          <name>
            <sname>Sname9</sname>
            <g-names>Fname9</g-names>
          </name>
          <email>email9@email.com</email>
          <xref ref-type="aff" rid="aff9" />
        </ctrb>
        <ctrb ctrb-type="au">
          <role content-type="10" />
          <name>
            <sname>Sname10</sname>
            <g-names>Fname10</g-names>
          </name>
          <email>email10@email.com</email>
          <xref ref-type="aff" rid="aff10" />
        </ctrb>
        <ctrb ctrb-type="edit">
          <name>
            <sname>SnameE1</sname>
            <g-names>FnameE1</g-names>
          </name>
          <degrees>PhD</degrees>
          <email>email1@mail.com</email>
          <role>EIC - OS</role>
          <xref ref-type="aff" rid="aff11" />
        </ctrb>

        <ctrb ctrb-type="edit">
          <name>
            <sname>SnameE2</sname>
            <g-names>FnameE2</g-names>
          </name>
          <degrees>BS</degrees>
          <email>email2@mail.com</email>
          <role>AE - OS</role>
          <xref ref-type="aff" rid="aff12" />
        </ctrb>
        <aff id="aff1">
          <institution>Uni1</institution>
          <adr-line content-type="adrline1">Addr Line 1</adr-line>
          <country>COUNTRY</country>
          <adr-line content-type="city">city1</adr-line>
          <adr-line content-type="zip">1000</adr-line>
          <phone content-type="primary">+123456789</phone>
        </aff>
        <aff id="aff2">
          <institution>Inst1</institution>
        </aff>
        <aff id="aff3">
          <institution>Inst2</institution>
        </aff>
        <aff id="aff4">
          <institution>Inst4</institution>
        </aff>
        <aff id="aff5">
          <institution>Inst5</institution>
        </aff>
        <aff id="aff6">
          <institution>Inst6</institution>
        </aff>
        <aff id="aff7">
          <institution>Inst7</institution>
        </aff>
        <aff id="aff8">
          <institution>Inst8</institution>
        </aff>
        <aff id="aff9">
          <institution>Inst9</institution>
        </aff>
        <aff id="aff10">
          <institution>Inst10</institution>
        </aff>
        <aff id="aff11">
          <institution>Uni2</institution>
          <institution content-type="position">Professor</institution>
          <institution content-type="dept">Department of BO</institution>
          <adr-line content-type="addrline1">480 MCP</adr-line>
          <country>LK</country>
          <adr-line content-type="city">City2</adr-line>
          <adr-line content-type="state">AP</adr-line>
        </aff>
        <aff id="aff12">
          <institution>Uni3</institution>
          <institution content-type="position">Associate Professor</institution>
          <institution content-type="dept">Department of GS</institution>
          <adr-line content-type="addrline1">1 Circle</adr-line>
          <adr-line content-type="addrline2">Building 4</adr-line>
          <country>LK</country>
          <adr-line content-type="city">SA</adr-line>
          <adr-line content-type="state">TC</adr-line>
          <adr-line content-type="zipcode">75289</adr-line>
        </aff>
      </c-group>
      <history>
        <date date-type="received">
          <day>18</day>
          <month>08</month>
          <year>2013</year>
        </date>
        <date date-type="rev-recd">
          <day>16</day>
          <month>07</month>
          <year>2016</year>
        </date>
        <date date-type="rev-recd">
          <day>07</day>
          <month>02</month>
          <year>2016</year>
        </date>
        <date date-type="rev-recd">
          <day>22</day>
          <month>10</month>
          <year>2015</year>
        </date>
        <date date-type="accepted">
          <day>12</day>
          <month>06</month>
          <year>2016</year>
        </date>
      </history>
      <kwd-group>
        <kwd>SI</kwd>
        <kwd>Air</kwd>
        <kwd>Bub</kwd>
      </kwd-group>
      <funding-group />
      <counts>
        <fig-count count="0" />
      </counts>
      <c-meta-group>
        <c-meta>
          <meta-value>MK</meta-value>
        </c-meta>
        <c-meta>
          <meta-name>Black and White Image Count</meta-name>
          <meta-value>0</meta-value>
        </c-meta>
        <c-meta>
          <meta-name>Color Image Count</meta-name>
          <meta-value>0</meta-value>
        </c-meta>
        <c-meta>
          <meta-name>Production Notes</meta-name>
          <meta-value>Hi,

mail goes here.

Thanks.
</meta-value>
        </c-meta>
        <c-meta id="SF">
          <meta-name>Special Feature Title</meta-name>
          <meta-value>BEP</meta-value>
        </c-meta>
        <c-meta id="Pub_charge">
          <meta-name>PC</meta-name>
          <meta-value>123</meta-value>
        </c-meta>
        <c-meta id="Discount">
          <meta-name>Discount</meta-name>
          <meta-value>55%</meta-value>
        </c-meta>
        <c-meta id="Explanation">
          <meta-name>Explanation</meta-name>
          <meta-value>SFA</meta-value>
        </c-meta>
      </c-meta-group>
    </a-meta>
  </first>
</at>

我希望按照下面的顺序显示XML文件中的文本(只需要显示来自少数标记的文本)。

代码语言:javascript
复制
<ctrb ctrb-type="edit">
    <name>
        <sname>SnameE1</sname>
            <g-names>FnameE1</g-names>
    </name>
    <xref ref-type="aff" rid="aff11" />
</ctrb>

<aff id="aff11">
          <institution>Uni2</institution>
</aff>


<ctrb ctrb-type="edit">
          <name>
            <sname>SnameE2</sname>
            <g-names>FnameE2</g-names>
          </name>
          <xref ref-type="aff" rid="aff12" />
 </ctrb>

<aff id="aff12">
          <institution>Uni3</institution>
 </aff>

要搜索每个<ctrb ctrb-type="edit">标记各自的<aff>标记,需要使用<xref>标记的rid属性值文本中的引用。我不确定是否可以使用XSL,因为我不是XSL专家。如果你们能在这件事上帮我,我会很感激的。

下面是我需要的HTML输出(简化):

代码语言:javascript
复制
<p>FnameE1 SnameE1</p>
<p>Uni2</p>
<br/>
<p>FnameE2 SnameE2</p>
<p>Uni3</p>

到目前为止,我所写的是(简化):

代码语言:javascript
复制
<p>Name: <xsl:value-of select="/at/first/a-meta/c-group/ctrb[@ctrb-type='edit']/name/g-names" />&#x00A0;<xsl:value-of select="/article/front/article-meta/contrib-group/contrib[@contrib-type='editor']/name/sname" /></p>
<p>Degree: <xsl:value-of select="/at/first/a-meta/c-group/ctrb[@ctrb-type='edit']/degrees" /></p>
<p>Email: <xsl:value-of select="/at/first/a-meta/c-group/ctrb[@ctrb-type='edit']/emai" /></p>
<p>Role: <xsl:value-of select="/at/first/a-meta/c-group/ctrb[@ctrb-type='edit']/role" /></p>

我想列出<ctrb ctrb-type="edit">标记的文本,并在每个<ctrb ctrb-type="edit">标记文本下查找和显示每个<ctrb ctrb-type="edit">的各自的<ctrb ctrb-type="edit">标记。任何逻辑或指导都会对我有很大的帮助。

如果你对我的问题不清楚,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-04 11:50:53

您的问题仍然令人困惑,因为预期的输出与您试图做的不匹配。

无论如何,试着把这个作为你的出发点:

XSLT1.0

代码语言:javascript
复制
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>

<xsl:key name="aff" match="aff" use="@id"/>

<xsl:template match="/at">
    <html>
        <body>
            <xsl:for-each select="first/a-meta/c-group/ctrb[@ctrb-type='edit']">
                <p>
                    <xsl:value-of select="name/sname"/>
                    <xsl:text>, </xsl:text>
                    <xsl:value-of select="name/g-names"/>
                </p>
                <p>
                    <xsl:value-of select="key('aff', xref/@rid)/institution"/>
                </p>
                <br/>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,结果将是:

代码语言:javascript
复制
<html>
   <body>
      <p>SnameE1, FnameE1</p>
      <p>Uni2</p>
      <br>
      <p>SnameE2, FnameE2</p>
      <p>Uni3</p>
      <br>
   </body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38156573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档