2016-08-17 45 views
2

这是我的XML文件(Input.xml文件)将XML文件转换成另一个XML文件:使用XSLT,但是代码的一部分不工作

<root> 
    <_shards> 
     <total>5</total> 
     <failed>0</failed> 
     <successful>5</successful> 
    </_shards> 
    <hits> 
     <hits> 
      <_index>PIB</_index> 
      <_type>SearchTech</_type> 
      <_source> 
       <extension> 

        <X-Parsed-By>org.apache.tika.parser.DefaultParser</X-Parsed-By> 

        <google-site-verification>jP1bIfjuuyZUYfTkYc_O6ZlTHxCm07voTDcMk72Z8oQ 
        </google-site-verification> 
        <Content-Location>http://www.example.com/big-data-search-managed-services-questions 
        </Content-Location> 
        <resourceName>http://www.example.com/big-data-search-managed-services-questions 
        </resourceName> 

        <X-UA-Compatible>IE=edge</X-UA-Compatible> 
        <viewport>width=device-width, initial-scale=1, maximum-scale=1 
        </viewport> 
        <Content-Encoding>UTF-8</Content-Encoding> 

        <format-detection>telephone=no</format-detection> 
       </extension> 
       <keywords>keywords-NOT-PROVIDED</keywords> 
       <secureFlag>false</secureFlag> 
       <description>How do you make sure your search and big data 
        applications are well-maintained? How to free IT staff&apos;s time 
        to focus more on business goals? Watch our expert address the top 5 
        managed services questions asked by customers. 
       </description> 
       <connectorSpecific> 
        <discoveredBy>http://www.example.com/enterprise-search-case-studies 
        </discoveredBy> 
        <xslt>false</xslt> 
        <pathFromSeed>LL</pathFromSeed> 
        <md5>WVBM5NDACLWRN4OSXRCQUA5RJA</md5> 
       </connectorSpecific> 
       <title>Do I Need Managed Services for My Search and Big Data 
        Applications? 
       </title> 
       <url>http://www.example.com/big-data-search-managed-services-questions 
       </url> 
       <UD>http://www.example.com/big-data-search-managed-services-questions 
       </UD> 
       <displayurl>http://www.example.com/big-data-search-managed-services-questions 
       </displayurl> 
       <UE>http://www.example.com/big-data-search-managed-services-questions 
       </UE> 
       <submitTime>2016-07-21T22:55:00+0000</submitTime> 

       <T>Do I Need Managed Services for My Search and Big Data 
        Applications? 
       </T> 
       <sourceType>heritrix</sourceType> 
       <U>http://www.example.com/big-data-search-managed-services-questions 
       </U> 
       <sourceName>PIB</sourceName> 
      </_source> 
      <_id>http://www.example.com/big-data-search-managed-services-questions 
      </_id> 
      <_score>1</_score> 
     </hits> 
     <hits> 
      <_index>paintitblack</_index> 
      <_type>SearchTech</_type> 
      <_source> 
       <extension> 
        <X-Parsed-By>org.apache.tika.parser.EmptyParser</X-Parsed-By> 
        <resourceName>http://www.example.com/ytPopup/aWcqZS7pbJQ 
        </resourceName> 
       </extension> 
       <keywords>keywords-NOT-PROVIDED</keywords> 
       <secureFlag>false</secureFlag> 
       <description>description-NOT-PROVIDED</description> 
       <connectorSpecific> 
        <discoveredBy>http://www.example.com/search-big-data-videos 
        </discoveredBy> 
        <xslt>false</xslt> 
        <pathFromSeed>LL</pathFromSeed> 
        <md5>2QOYZWMPACZAJ2MABGMOZ6CCPY</md5> 
       </connectorSpecific> 
       <title>Title-NOT-PROVIDED</title> 
       <url>http://www.example.com/ytPopup/aWcqZS7pbJQ</url> 
       <UD>http://www.example.com/ytPopup/aWcqZS7pbJQ</UD> 
       <displayurl>http://www.example.com/ytPopup/aWcqZS7pbJQ 
       </displayurl> 
       <UE>http://www.example.com/ytPopup/aWcqZS7pbJQ</UE> 
       <submitTime>2016-07-21T22:56:22+0000</submitTime> 
       <newContent>newContent-NOT-PROVIDED</newContent> 
       <sourceType>heritrix</sourceType> 
       <U>http://www.example.com/ytPopup/aWcqZS7pbJQ</U> 
       <sourceName>PIB</sourceName> 
      </_source> 
      <_id>http://www.example.com/ytPopup/aWcqZS7pbJQ</_id> 
      <_score>1</_score> 
     </hits> 
     <total>1036</total> 
     <max_score>1</max_score> 
    </hits> 
    <took>3</took> 
    <timed_out>false</timed_out> 
</root> 

这是我的XSLT(Convert.XSLT):

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:fn="http://www.w3.org/2005/xpath-functions" 
    exclude-result-prefixes="xs fn"> 
    <xsl:output method="xml" encoding="UTF-8" byte-order-mark="no" 
     indent="yes" /> 
    <xsl:template match="/"> 
     <GSP> 
      <xsl:attribute name="VER"> 
       <xsl:value-of select="3.2" /> 
      </xsl:attribute> 
      <xsl:for-each select="root"> 
       <TM> 
        <xsl:value-of select="(floor(took) div floor(1000))" /> 
       </TM> 

       <RES> 
        <M> 
         <xsl:value-of select="floor(hits/total)" /> 
        </M> 
        <xsl:for-each select="hits/hits"> 
         <xsl:variable name="var1_resultof_first" as="node()" 
          select="_source" /> 
         <R> 
          <xsl:attribute name="N"> 
          <xsl:number format="0" level="single" /> 
          </xsl:attribute> 
          <U> 
           <xsl:sequence 
            select="xs:string(xs:anyURI(fn:string($var1_resultof_first/U)))" /> 
          </U> 
          <UE> 
           <xsl:sequence 
            select="xs:string(xs:anyURI(fn:string($var1_resultof_first/UE)))" /> 
          </UE> 
          <UD> 
           <xsl:sequence 
            select="xs:string(xs:anyURI(fn:string($var1_resultof_first/UD)))" /> 
          </UD> 
          <T> 
           <xsl:sequence select="fn:string($var1_resultof_first/T)" /> 
          </T> 
          <Last-Modified> 
           <xsl:value-of select="substring-before(_source/submitTime,'T')" /> 
          </Last-Modified> 
          <S> 
           <xsl:for-each select="highlight/newContent"> 
            <xsl:sequence select="fn:string(.)" /> 
           </xsl:for-each> 
          </S> 

          <MT> 
           <xsl:attribute name="N">X-Parsed-By</xsl:attribute> 
           <xsl:attribute name="V"><xsl:value-of select="extension/X-Parsed-By"/></xsl:attribute> 
          </MT> 

         </R> 
        </xsl:for-each> 
       </RES> 
      </xsl:for-each> 
     </GSP> 
    </xsl:template> 
</xsl:stylesheet> 

而且我的输出(与Output.xml)看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<GSP VER="3.2"> 
    <TM>0.003</TM> 
    <RES> 
     <M>1036</M> 
     <R N="1"> 
     <U>http://www.example.com/big-data-search-managed-services-questions</U> 
     <UE>http://www.example.com/big-data-search-managed-services-questions</UE> 
     <UD>http://www.example.com/big-data-search-managed-services-questions</UD> 
     <T>Do I Need Managed Services for My Search and Big Data Applications?</T> 
     <Last-Modified>2016-07-21</Last-Modified> 
     <S/> 
     <MT N="X-Parsed-By" V=""/> 
     </R> 
     <R N="2"> 
     <U>http://www.example.com/ytPopup/aWcqZS7pbJQ</U> 
     <UE>http://www.example.com/ytPopup/aWcqZS7pbJQ</UE> 
     <UD>http://www.example.com/ytPopup/aWcqZS7pbJQ</UD> 
     <T/> 
     <Last-Modified>2016-07-21</Last-Modified> 
     <S/> 
     <MT N="X-Parsed-By" V=""/> 
     </R> 
    </RES> 
</GSP> 

你会看到我的输出标签MT和它的属性“V”不具有y值:

<MT N="X-Parsed-By" V=""/> 

我的XSLT的下面部分不起作用。属性名称“V”不会提取我的XML元素“X-Parsed-By”的值。该计划的其他部分工作正常。

<MT> 
<xsl:attribute name="N">X-Parsed-By</xsl:attribute> 
<xsl:attribute name="V"><xsl:value-of select="extension/X-Parsed-By"/></xsl:attribute> 
</MT> 

我希望我的输出(与Output.xml)看起来像这样..

..... 
<MT N="X-Parsed-By" V="org.apache.tika.parser.EmptyParser"/> 
... 

任何帮助,将不胜感激

回答

2

变化:

<xsl:attribute name="V"><xsl:value-of select="extension/X-Parsed-By"/></xsl:attribute> 

到:

<xsl:attribute name="V"><xsl:value-of select="_source/extension/X-Parsed-By"/></xsl:attribute> 

注意,因为你正在使用XSLT 2.0,您可以缩短这:

<xsl:attribute name="V" select="_source/extension/X-Parsed-By"/> 

更妙的是,使用attribute value template,缩短了整个事情:

<MT N="X-Parsed-By" V="{_source/extension/X-Parsed-By}"/>