转换多个文档上下文

时间:2014-03-17 06:21:39

标签: xml xslt xslt-1.0 document

我有一个使用外部文件的xsl。 xsl包含xsl:key元素,当使用外部文档时,这不能正常工作。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="html" encoding="utf-8"/>
  <xsl:param name="People"></xsl:param>
  <xsl:param name="Abstracts"></xsl:param>
  <xsl:key name="uniqueAfflTags" match="AFFL" use="concat(../../../../@id,@number)"/>
  <xsl:variable name="abstracts" select="document($Abstracts)//ABSTRACT"/>

<!--Root template -->
  <xsl:template match="/">
    <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
      </head>
      <body>
        <xsl:apply-templates select="//PRESENTATIONS/PRESENTATION[@type!='placeholder']">
          <xsl:sort select="AUTHORS/AUTHOR[1]/LNAME"/>
        </xsl:apply-templates>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="PRESENTATION">
    <xsl:variable name="ctrlid" select="@control_id"/>
    <xsl:text> </xsl:text>
    <!--  Category -->
    <xsl:if test="string-length($abstracts[@id=$ctrlid]/ABSTRACT_CATEGORY/CATEGORY_GROUP/CATEGORY) &gt; 0">
      <xsl:text> - </xsl:text>
      <span class="Category">
        <xsl:value-of select="$abstracts[@id=$ctrlid]/ABSTRACT_CATEGORY/CATEGORY_GROUP/CATEGORY"/>
      </span>
    </xsl:if>
    <xsl:apply-templates select="descendant::AFFL">
      <xsl:sort select="@number" data-type="number"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="AFFL">
    <xsl:if test="generate-id()=generate-id(key('uniqueAfflTags',concat(../../../../@id,@number))[1])">
      (<xsl:value-of select="@number"/>)
      <xsl:if test="string-length(DEPT) &gt; 0">
        <xsl:value-of select="DEPT"/>
      </xsl:if>

      <xsl:if test="string-length(INST) &gt; 0">
        <xsl:if test="string-length(DEPT) &gt; 0">
          <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:value-of select="INST"/>
      </xsl:if>

      <xsl:if test="string-length(CITY) &gt; 0">
        <xsl:if test="string-length(INST) &gt; 0 or string-length(DEPT) &gt; 0">
          <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:value-of select="CITY"/>
      </xsl:if>

      <xsl:if test="string-length(STATE) &gt; 0">
        <xsl:if test="string-length(CITY) &gt; 0 or string-length(INST) &gt; 0 or string-length(DEPT) &gt; 0">
          <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:value-of select="STATE"/>
      </xsl:if>

      <xsl:if test="string-length(COUNTRY) &gt; 0">
        <xsl:if test="COUNTRY!='United States'">
          <xsl:if test="string-length(STATE) &gt; 0 or string-length(CITY) &gt; 0 or string-length(INST) &gt; 0 or string-length(DEPT) &gt; 0">
            <xsl:text>, </xsl:text>
          </xsl:if>
          <xsl:value-of select="COUNTRY"/>
        </xsl:if>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template match="text()">
  </xsl:template>
</xsl:stylesheet>

使用当前的xsl,AFFL模板匹配中的if条件不满意。如果我删除这些行:

<xsl:variable name="ctrlid" select="@control_id"/>
    <xsl:text> </xsl:text>
    <!--  Category -->
    <xsl:if test="string-length($abstracts[@id=$ctrlid]/ABSTRACT_CATEGORY/CATEGORY_GROUP/CATEGORY) &gt; 0">
      <xsl:text> - </xsl:text>
      <span class="Category">
        <xsl:value-of select="$abstracts[@id=$ctrlid]/ABSTRACT_CATEGORY/CATEGORY_GROUP/CATEGORY"/>
      </span>
    </xsl:if>

它开始工作。问题看起来像是不正确的上下文,但我无法正确设置上下文。 很感谢任何形式的帮助。 感谢

输入xml - &gt; primary xml(session)

<PRESENTATION id="1424043" sess_sort="10" control_id="1841983" type="abstract">
          <FINAL_ID><![CDATA[]]></FINAL_ID>
          <DURATION><![CDATA[20]]></DURATION>
          <PRESENTATION_START_TIME>
            <HOUR><![CDATA[04]]></HOUR>
            <MINUTE><![CDATA[05]]></MINUTE>
            <AM_PM><![CDATA[PM]]></AM_PM>
          </PRESENTATION_START_TIME>
          <PRESENTATION_END_TIME>
            <HOUR><![CDATA[04]]></HOUR>
            <MINUTE><![CDATA[25]]></MINUTE>
            <AM_PM><![CDATA[PM]]></AM_PM>
          </PRESENTATION_END_TIME>
          <TITLE><![CDATA[Rift Structures in the Angola Margin: Block 0]]></TITLE>
          <STATUS><![CDATA[Sessioned]]></STATUS>
          <DECISION_STATUS><![CDATA[Accept]]></DECISION_STATUS>
          <DESC><![CDATA[]]></DESC>
          <USER_DEFINED_1><![CDATA[false]]></USER_DEFINED_1>
          <AUTHORS>
            <AUTHOR order="1" person_id="2720052" presenter="true">
              <FNAME><![CDATA[Li-Fan]]></FNAME>
              <MNAME><![CDATA[]]></MNAME>
              <LNAME><![CDATA[Yue]]></LNAME>
              <AFFILIATIONS>
                <AFFL author_order="1" number="1">
                  <DEPT><![CDATA[]]></DEPT>
                  <INST><![CDATA[Chevron]]></INST>
                  <CITY><![CDATA[Houston]]></CITY>
                  <STATE><![CDATA[Texas]]></STATE>
                  <COUNTRY><![CDATA[United States]]></COUNTRY>
                </AFFL>
              </AFFILIATIONS>
            </AUTHOR>
            <AUTHOR order="2" person_id="2713558" presenter="false">
              <FNAME><![CDATA[Chris]]></FNAME>
              <MNAME><![CDATA[]]></MNAME>
              <LNAME><![CDATA[Guzofski]]></LNAME>
              <AFFILIATIONS>
                <AFFL author_order="1" number="1">
                  <DEPT><![CDATA[]]></DEPT>
                  <INST><![CDATA[Chevron]]></INST>
                  <CITY><![CDATA[Houston]]></CITY>
                  <STATE><![CDATA[Texas]]></STATE>
                  <COUNTRY><![CDATA[United States]]></COUNTRY>
                </AFFL>
              </AFFILIATIONS>
            </AUTHOR>
          </AUTHORS>
          <BODY>
            <SECTION part_of_body="1">
              <SECTION_NAME bold="true" italic="false" underline="false" name_appears="false"><![CDATA[Abstract Body]]></SECTION_NAME>
              <TEXT><![CDATA[
Angola Block 0 has long been a prolific asset for Angola and Chevron ever since the Cabinda Gulf Oil Company drilled the first well in the late 
]]></TEXT>
            </SECTION>
          </BODY>
        </PRESENTATION>

第二个xml(抽象XML) -

<ABSTRACT id="1785505">
      <STATUS><![CDATA[Sessioned]]></STATUS>
      <DECISION_STATUS><![CDATA[Accept]]></DECISION_STATUS>
      <TITLE><![CDATA[Fault controlled extension regime– a case of the Albertine Graben - East African Rift System, Uganda]]></TITLE>
      <PRESENTATION_TYPE><![CDATA[Oral Only]]></PRESENTATION_TYPE>
      <ABSTRACT_CATEGORY>
        <CATEGORY_GROUP>
          <CATEGORY category_id="136962"><![CDATA[Theme 8: Structure, Tectonics, and Geomechanics]]></CATEGORY>
          <SUB_CATEGORY sub_category_id="137018"><![CDATA[3. Structure and Geodynamics of Extensional Margins and Impact on the Evolution of Petroleum Systems and Reservoir Productivity]]></SUB_CATEGORY>
        </CATEGORY_GROUP>
      </ABSTRACT_CATEGORY>
      <AUTHORS>
        <AUTHOR order="1" person_id="2646796" presenter="true">
          <SAL><![CDATA[Mr.]]></SAL>
          <FNAME><![CDATA[Dozith]]></FNAME>
          <MNAME><![CDATA[]]></MNAME>
          <LNAME><![CDATA[Abeinomugisha]]></LNAME>
          <DEGREE><![CDATA[Msc Petroleum Geoscience]]></DEGREE>
          <EMAIL><![CDATA[d.abeinomugisha@petroleum.go.ug]]></EMAIL>
          <ADDRESS_1><![CDATA[Plot 21-29, End of Johnstone road, Entebbe]]></ADDRESS_1>
          <ADDRESS_2><![CDATA[]]></ADDRESS_2>
          <CITY><![CDATA[Entebbe]]></CITY>
          <STATE><![CDATA[]]></STATE>
          <POSTAL_CODE><![CDATA[9]]></POSTAL_CODE>
          <COUNTRY><![CDATA[Uganda]]></COUNTRY>
          <PHONE><![CDATA[+256414320714]]></PHONE>
          <FAX><![CDATA[+256414320437]]></FAX>
          <AFFILIATIONS>
            <AFFL author_order="1" number="1">
              <DEPT><![CDATA[Geology]]></DEPT>
              <INST><![CDATA[Petroleum Exploration and Production Department]]></INST>
              <CITY><![CDATA[Entebbe]]></CITY>
              <STATE><![CDATA[]]></STATE>
              <COUNTRY><![CDATA[Uganda]]></COUNTRY>
            </AFFL>
          </AFFILIATIONS>
        </AUTHOR>
      </AUTHORS>
      <BODY>
        <SECTION part_of_body="1">
          <SECTION_NAME bold="false" italic="false" underline="false" name_appears="false"><![CDATA[Abstract Body]]></SECTION_NAME>
          <TEXT><![CDATA[The Albertine graben forms the northernmost termination of the western arm of the East African Rift System. Rifting was initiated.
]]></TEXT>
        </SECTION>
      </BODY>
    </ABSTRACT>

如果它确实是一个上下文问题,那么我该如何选择基础文档的上下文。

1 个答案:

答案 0 :(得分:1)

密钥索引是按文档构建的,密钥函数适用于当前上下文节点的文档,因此通常需要多个文档来确保使用例如更改上下文。

<xsl:variable name="some-key-value" select="foo"/>
<xsl:for-each select="$otherDoc">
  <xsl:apply-templates select="key('key-name', $some-key-value)"/>
</xsl:for-each>

在XSLT 1.0或XSLT 2.0中,您可以将其缩短为

<xsl:apply-templates select="key('key-name', foo, $otherDoc)"/>

如果您需要更具体的帮助,请告诉我们您输入文档的结构。