当前位置: 动力学知识库 > 问答 > 编程问答 >

Converting XML file into another XML file using XSLT but one part of the code is not working

问题描述:

This is my XML file(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>

This is my 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>

And my output(Output.xml) looks like this:

<?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>

You will see the tag MT in my output and its attribute "V" doesn't have any value:

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

The below part of my XSLT is not working. The attribute name "V" is not extracting the value of my XML element "X-Parsed-By". Other part of the program is working fine.

<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>

I want my output(Output.xml) to look like this..

.....

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

...

Any help would be appreciated

网友答案:

Change:

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

to:

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

Note that since you are using XSLT 2.0, you can shorten this to:

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

Even better, use an attribute value template and shorten the whole thing to:

<MT N="X-Parsed-By" V="{_source/extension/X-Parsed-By}"/>
分享给朋友:
您可能感兴趣的文章:
随机阅读: