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

标签 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,并且其属性“V”没有任何值:

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

如有任何帮助,我们将不胜感激

最佳答案

更改:

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

关于xml - 使用 XSLT 将 XML 文件转换为另一个 XML 文件,但部分代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39006498/

相关文章:

xml - xpath/xslt相等检查多个值

python - 关于正则表达式和 XML

html -> 有必要吗?

java - 了解简单 XML 中的构造函数注入(inject)

c# - 将参数传递给 XSLT 样式表

html - XSLT:for-each 中的 for-each 不起作用?

xml - 使用 XSLT 1 根据另一个 xml 过滤 XML

android - 使用 RadioButton/RadioGroup 行为创建 TextView(反之亦然)

.net - 从 XSLT 输出根中删除 XML 命名空间声明?

xslt - 如何将模板应用到除特定模板之外的所有模板