XSLT误解了document()函数

时间:2014-12-22 15:56:25

标签: xml xslt xpath

我正在尝试合并2个xml文档,在第一个文档中获取一些属性值,然后将其注入另一个中。

我的第一个xml:Manual.xml有这样的形式:我有类似的5 000个任务:

           <task chapnbr="05"
                 sectnbr="11"
                 subjnbr="04"
                 pgblknbr="08"
                 breaknbr="001"
                 confnbr=""
                 confgtxt=""
                 func="200"
                 seq="001"
                 confltr=""
                 chg="u"
                 key="t051104200001"
                 revdate="20070615">
              <effect effrg="ALL">

我处理的第二个xml:Dictionary.xml:

<dictionary engine="CFM56" version="3" manual="ESM">
   <task oldkey="T050000870001"
         newkey="TKESM050000870001"
         chapnbr="05"
         sectnbr="00"
         subjnbr="00"
         func="870"
         seq="001">
      <title>GENERAL - INTRODUCTION</title>
   </task>
   <task oldkey="T051100200001"
         newkey="TKESM051100200001"
         chapnbr="05"
         sectnbr="11"
         subjnbr="00"
         func="200"
         seq="001">
      <title>LIFE LIMITS OF ENGINE ROTATING PARTS</title>
   </task>
   <task oldkey="T051101200001"
         newkey="TKESM051101200001"
         chapnbr="05"
         sectnbr="11"
         subjnbr="01"
         func="200"
         seq="001">
      <title>FAN ROTOR - LIFE LIMITS</title>
   </task>
   <task oldkey="T051102200001"
         newkey="TKESM051102200001"
         chapnbr="05"
         sectnbr="11"
         subjnbr="02"
         func="200"
         seq="001">
      <title>HIGH PRESSURE COMPRESSOR ROTOR - LIFE LIMITS</title>
</dictionary>

我需要获取属性的值&#34; seq&#34;如果属性&#34; oldkey&#34;在词典中并在手册中替换它。在词典中等于属性&#34; key&#34;在手册中。

要做到这一点,我已经写了这个XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">

<xsl:output 
    encoding="UTF-8"
    method="xml" 
    indent="yes" 
    omit-xml-declaration="yes" 
    cdata-section-elements="comments"/>


<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@seq[parent::task]">
    <xsl:variable name="oldkey" select="upper-case(../@key)"/>
    <xsl:attribute name="seq">
        <xsl:value-of select="document('./Dictionary.xml')/dictionary/task[@oldkey=$oldkey]/@seq"/>
    </xsl:attribute>
</xsl:template>  

</xsl:stylesheet>

但我得到的结果是&#34; seq&#34;的所有值。更改并替换为手册中的空白。这是输出的一个例子:

<task chapnbr="72"
                 sectnbr="00"
                 subjnbr="00"
                 pgblknbr="01"
                 breaknbr="011"
                 confnbr=""
                 confgtxt=""
                 func="810"
                 seq=""
                 confltr=""
                 chg="u"
                 key="t720000810011"
                 revdate="19981015">

我想我的问题来自于我使用document()函数的方式,但我尝试过很多东西而且没有任何变化。结果总是一样的,我不会设法获得另一个值,除了属性中的空白&#34; seq&#34;我的输出文件手册。

我需要获得一个输出,其中属性&#34; seq&#34; value由3个整数组成,并从Dictionnary中读取。有点像:

<task chapnbr="72"
                 sectnbr="00"
                 subjnbr="00"
                 pgblknbr="01"
                 breaknbr="011"
                 confnbr=""
                 confgtxt=""
                 func="810"
                 seq="001"
                 confltr=""
                 chg="u"
                 key="t720000810011"
                 revdate="19981015">

更准确地了解我的输入/输出文件(Manual.xml)。完整文件的格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE EM [<!ENTITY g1105335-00 SYSTEM " file://D:/Indigo_AP/Load_CFM56-3/5-   Inputs/../Arts/1105335-00.tif" NDATA ccitt4>
<!ENTITY g1105336-00 SYSTEM " file://D:/Indigo_AP/Load_CFM56-3/5-Inputs/../Arts/1105336-00.tif" NDATA ccitt4 >

<!-- A lot of Doctype declaration -->

<em spl="062W0"
model="CFM56-3"
oidate="19831231"
tsn="71"
docnbr="CFMI-TP_SM_5"
lang="english"
revdate="20131215"
chg="u">
   <title>CFM56-3 Engine Manual</title>

   <!-- Many child elements -->

   <chapter chapnbr="05" chg="u" key="c05" revdate="20121215">
      <title>TIME LIMITS AND MAINTENANCE CHECKS</title>

      <section chapnbr="05"
           sectnbr="00"
           chg="u"
           key="cs0500"
           revdate="20121215">
         <title>GENERAL</title>

         <subject chapnbr="05"
              sectnbr="00"
              subjnbr="00"
              chg="u"
              key="css050000"
              revdate="20121215">
            <title>GENERAL - INTRODUCTION</title>

            <pgblk chapnbr="05"
               sectnbr="00"
               subjnbr="00"
               pgblknbr="00"
               breaknbr="001"
               chg="u"
               key="p05000000001000"
               revdate="20121215">
              <title>GENERAL - INTRODUCTION</title>

               <task chapnbr="05"
                 sectnbr="00"
                 subjnbr="00"
                 pgblknbr="00"
                 breaknbr="001"
                 confnbr=""
                 confgtxt=""
                 func="870"
                 seq="001"
                 confltr=""
                 chg="u"
                 key="t050000870001"
                 revdate="20121215">
                  <effect effrg="ALL"></effect>
                  <title>GENERAL - INTRODUCTION</title>

                  <!-- Many child elements -->

           </task>
        </pgblk>
     </subject>
  </section>

  <section chapnbr="05"
           sectnbr="11"
           chg="u"
           key="cs0511"
           revdate="20121215">
     <title>LIFE LIMITS</title>

     <subject chapnbr="05"
              sectnbr="11"
              subjnbr="00"
              chg="u"
              key="css051100"
              revdate="20070615">
        <title>LIFE LIMITS</title>

        <pgblk chapnbr="05"
               sectnbr="11"
               subjnbr="00"
               pgblknbr="08"
               breaknbr="001"
               chg="u"
               key="p05110008001000"
               revdate="20070615">
           <title>LIFE LIMITS OF ENGINE ROTATING PARTS</title>

           <task chapnbr="05"
                 sectnbr="11"
                 subjnbr="00"
                 pgblknbr="08"
                 breaknbr="001"
                 confnbr=""
                 confgtxt=""
                 func="200"
                 seq="001"
                 confltr=""
                 chg="u"
                 key="t051100200001"
                 revdate="20070615">
              <effect effrg="ALL"></effect>
              <title>LIFE LIMITS OF ENGINE ROTATING PARTS</title>

              <!-- Many child elements -->

           </task>
        </pgblk>
     </subject>

     <subject chapnbr="05"
              sectnbr="11"
              subjnbr="01"
              chg="u"
              key="css051101"
              revdate="20070615">
        <title>FAN ROTOR - LIFE LIMITS</title>

        <pgblk chapnbr="05"
               sectnbr="11"
               subjnbr="01"
               pgblknbr="08"
               breaknbr="001"
               chg="u"
               key="p05110108001000"
               revdate="20070615">
           <title>FAN ROTOR - LIFE LIMITS</title>

           <task chapnbr="05"
                 sectnbr="11"
                 subjnbr="01"
                 pgblknbr="08"
                 breaknbr="001"
                 confnbr=""
                 confgtxt=""
                 func="200"
                 seq="001"
                 confltr=""
                 chg="u"
                 key="t051101200001"
                 revdate="20070615">
              <effect effrg="ALL"></effect>
              <title>FAN ROTOR - LIFE LIMITS</title>

              <!-- Many child elements -->

               </task>
            </pgblk>
         </subject>
      </section>
   </chapter>
</em>

1 个答案:

答案 0 :(得分:0)

我建议您运行以下样式表(针对任何格式良好的XML输入),看看你得到了什么:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="dict" select="document('Dictionary.xml')" />
<xsl:variable name="key" select="'T050000870001'" />

<xsl:template match="/">
    <results>
        <dictionary-exists>
            <xsl:value-of select="boolean($dict)"/>
        </dictionary-exists>
        <entry-exists>
            <xsl:value-of select="boolean($dict/dictionary/task[@oldkey=$key])"/>
        </entry-exists>
        <value>
            <xsl:value-of select="$dict/dictionary/task[@oldkey=$key]/@seq"/>
        </value>
    </results>
</xsl:template>

</xsl:stylesheet>