使用XSLT将XML文件转换为另一个XML文件,但代码的一部分不起作用

时间:2016-08-17 21:22:29

标签: xml xslt

这是我的XML文件(Input.xml):

<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及其属性&#34; V&#34;没有任何价值:

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

我的XSLT的下面部分无法正常工作。属性名称&#34; V&#34;没有提取我的XML元素的值&#34; X-Parsed-By&#34;。该计划的其他部分工作正常。

<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"/>
...

任何帮助将不胜感激

1 个答案:

答案 0 :(得分: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}"/>