如何在首次运行时使用内部节点后返回xml中的根节点?

时间:2017-05-04 08:58:05

标签: xml xslt xpath

这是我的xslt代码,在运行之后,只有吸收的公司显示在pdf中,但其他xsl值不是。如何返回根节点以便我可以使用其他xsl值或使它们出现?

要明确根节点是“secCertificateModel”,那么像“secNumber”这样的其他节点位于根节点下。

我立即使用吸收公司/吸收公司,因为我想为每个吸收的公司打印一个全新的pdf。

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
<xsl:template match="secCertificateModel">


    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="Tahoma">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
          <fo:region-body/>
          <fo:region-after region-name="footer" extent="0mm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simpleA4">


    <fo:static-content flow-name="footer">
      <fo:block text-align-last="start">
    <xsl:call-template name="MyFooter"/></fo:block>
    </fo:static-content>
        <fo:flow flow-name="xsl-region-body">

<xsl:for-each select="absorbedCompanies/absorbedCompany">


<fo:block keep-together.within-column="always">




         <fo:table space-after.optimum="0pt">
    <fo:table-column column-width="480pt" />


  <fo:table-body>  
    <fo:table-row>
      <fo:table-cell text-align="left" start-indent="0mm">

          <fo:block font-size="12pt" font-weight="bold" font-style="italic" text-align="end">  <xsl:value-of select="secNumber"/>
          </fo:block>
      </fo:table-cell>
    </fo:table-row>  


  </fo:table-body>
</fo:table>


          <fo:block font-size="14pt" font-weight="bold" text-align="center"  space-after.optimum="0pt"> <xsl:value-of select="survivingCompany/survivingCompanyName"/>&#xA0;<xsl:value-of select="survivingCompany/survivingSuffix"/>
          </fo:block>  
          <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="1pt"> <xsl:value-of select="survivingCompany/survivingDoingBusiness"/>
          </fo:block> 

          <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="0pt"> 
               <xsl:for-each select="survivingCompany/tradeName">
                        <xsl:value-of select="." />
                         <xsl:if test="position() &lt; last()-1">; </xsl:if>
                         <xsl:if test="position() = last()-1"> and </xsl:if>
                         <xsl:if test="position() = last()"></xsl:if>
          </xsl:for-each>
          </fo:block> 
          <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="10pt">( Surviving Corporation )
          </fo:block>  
          <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="10pt">and
          </fo:block>  


          <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="0pt"> <xsl:value-of select="absorbedCompanyName"/>&#xA0;<xsl:value-of select="absorbedSuffix"/>
          </fo:block>
          <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="1pt"> <xsl:value-of select="absorbedDoingBusiness"/>
          </fo:block> 
          <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="0pt"> 
            <xsl:for-each select="tradeName">
                        <xsl:value-of select="." />
                         <xsl:if test="position() &lt; last()-1">; </xsl:if>
                         <xsl:if test="position() = last()-1"> and </xsl:if>
                         <xsl:if test="position() = last()"></xsl:if>
          </xsl:for-each>
          </fo:block> 
          <fo:block font-size="14pt" font-weight="bold" text-align="center"  space-after.optimum="20pt">( 
ed Corporation )
          </fo:block> 

                         <xsl:if test="position() &lt; last()-1">
                              <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="10pt">and
                              </fo:block>   
                        </xsl:if>
                         <xsl:if test="position() = last()-1"> 
                            <fo:block font-size="14pt" font-weight="bold" text-align="center" space-after.optimum="10pt">and
                            </fo:block>   
                        </xsl:if>
                         <xsl:if test="position() = last()"></xsl:if>


          <fo:block font-size="14pt" text-align="justify" space-after.optimum="25pt"><xsl:value-of select="boiDate"/>, 


          <fo:inline font-weight="bold"> 

          <xsl:for-each select="absorbedCompanies/absorbedCompany">
                        <xsl:value-of select="absorbedCompanyName"/>&#xA0;<xsl:value-of select="absorbedSuffix"/>
                         <xsl:if test="position() &lt; last()-1">; </xsl:if>
                         <xsl:if test="position() = last()-1"> and </xsl:if>
                         <xsl:if test="position() = last()"></xsl:if>
          </xsl:for-each>

          </fo:inline>
           <fo:inline font-weight="bold"> <xsl:value-of select="survivingCompany/survivingCompanyName"/>&#xA0;<xsl:value-of select="survivingCompany/survivingSuffix"/>&#xA0;<xsl:value-of select="survivingCompany/survivingDoingBusiness"/>

           <xsl:for-each select="survivingCompany/tradeName">
                        <xsl:value-of select="." />
                         <xsl:if test="position() &lt; last()-1">; </xsl:if>
                         <xsl:if test="position() = last()-1"> and </xsl:if>
                         <xsl:if test="position() = last()"></xsl:if>
          </xsl:for-each>

          </fo:inline>

          </fo:block>


          <fo:block font-size="14pt" text-indent="15mm"  text-align="justify" space-after.optimum="45pt"> <fo:inline font-weight="bold" font-style="italic"></fo:inline> <xsl:value-of select="secOffice"/>,  <xsl:value-of select="certificateDate"/>.
          </fo:block> 





              </fo:block>

          </xsl:for-each> 

        </fo:flow>
      </fo:page-sequence>
     </fo:root>


</xsl:template>


    <xsl:template name="MyFooter">

        <fo:block font-size="8pt" text-align="start" ><xsl:value-of select="psicCode"/></fo:block>

    </xsl:template>


</xsl:stylesheet>

这是XML btw:`

<secCertificateModel>
<secNumber></secNumber>
<companyName></companyName>
<doingBusiness></doingBusiness>
<certificateDate></certificateDate>
<secOffice></secOffice>
<psicCode></psicCode>
<boiDate></boiDate>
<foreignCompanyName></foreignCompanyName>
<suffix></suffix>
<hasTradeNames></hasTradeNames>
<tradeNameBreak></tradeNameBreak>

<survivingCompany>
    <survivingCompanyName></survivingCompanyName>
    <survivingSuffix></survivingSuffix>
    <survivingDoingBusiness></survivingDoingBusiness>
    <tradeName></tradeName>
</survivingCompany>
<absorbedCompanies>
    <absorbedCompany>
        <absorbedCompanyName></absorbedCompanyName>
        <absorbedSuffix></absorbedSuffix>
        <absorbedDoingBusiness></absorbedDoingBusiness>
    </absorbedCompany>
    <absorbedCompany>
        <absorbedCompanyName></absorbedCompanyName>
        <absorbedSuffix></absorbedSuffix>
        <absorbedDoingBusiness></absorbedDoingBusiness>
    </absorbedCompany>

</absorbedCompanies>

`

1 个答案:

答案 0 :(得分:1)

您的模板与secCertificateModel根元素匹配。当你这样做时:

<xsl:for-each select="absorbedCompanies/absorbedCompany">

您将被置于absorbedCompany的上下文中,直到xsl:for-each块结束。然后返回到模板匹配模式建立的上下文。

要在absorbedCompany或其中一个后代 - 或实际上来自任何上下文 - 的上下文中引用根元素,您始终可以使用绝对路径;例如:

<xsl:value-of select="/secCertificateModel/secNumber"/>
无论当前上下文节点是什么,

都将返回secNumber的值。

另一种选择是使用和/或祖先轴;例如:

<xsl:value-of select="ancestor:secCertificateModel/secNumber"/>

将从除根节点和根元素之外的任何上下文返回secNumber的值。