Xslt返回空的xml

时间:2018-10-29 11:11:02

标签: xml xslt

我正在尝试使用xslt转换xml,但是不幸的是,我总是得到一个空的xml。我猜比赛有点问题

XML: https://pastebin.com/fV9Au20L

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
   <soap-env:Header/>
   <soap-env:Body>
      <n0:ZPmUltimoArtikelResponse xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style">
         <TArtikel>
            <item>
               <Niederlassung>TKD</Niederlassung>
               <Matnr>34459</Matnr>
               <Maktx>Nadelhülse       HK 5520</Maktx>
               <Meins>ST</Meins>
               <Preis>7.0</Preis>
               <Chargenpflicht/>
            </item>
            <item>
               <Niederlassung>TKD</Niederlassung>
               <Matnr>06182</Matnr>
               <Maktx>Buchse / --806</Maktx>
               <Meins>ST</Meins>
               <Preis>24.7</Preis>
               <Chargenpflicht/>
            </item>
         </TArtikel>
      </n0:ZPmUltimoArtikelResponse>
   </soap-env:Body>
</soap-env:Envelope>

xslt: https://pastebin.com/TGLYwWeL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" exclude-result-prefixes=" xml xsl" xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">Test</xsl:template>
    <xsl:template match="/soap-env:Envelope/soap-env:Body/n0:ZPmUltimoArtikelResponse/TArtikel">
        test2
        <xsl:for-each select="item">
        test3
            <Object Type="Article" Action="InsertOrSkip">
                <Property Name="Context" Value="1"/>
                <Property Name="Description">
                    <xsl:attribute name="Value"><xsl:value-of select="Maktx"/></xsl:attribute>
                </Property>

            </Object>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我希望你们能帮助我

1 个答案:

答案 0 :(得分:3)

在文档节点模板中,像这样apply-templates使用<xsl:template match="/"><xsl:apply-templates/></xsl:template>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xml="http://www.w3.org/XML/1998/namespace" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" 
    exclude-result-prefixes="xml xsl n0" 
    xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style">

    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="/soap-env:Envelope/soap-env:Body/n0:ZPmUltimoArtikelResponse/TArtikel">
        <!--test2-->
        <xsl:for-each select="item">
            <!--test3-->
            <Object Type="Article" Action="InsertOrSkip">
                <Property Name="Context" Value="1"/>
                <Property Name="Description">
                    <xsl:attribute name="Value"><xsl:value-of select="Maktx"/></xsl:attribute>
                </Property>

            </Object>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header/>
    <soap-env:Body>
        <n0:ZPmUltimoArtikelResponse xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style">
            <TArtikel>
                <item>
                    <Niederlassung>TKD</Niederlassung>
                    <Matnr>34459</Matnr>
                    <Maktx>Nadelhülse       HK 5520</Maktx>
                    <Meins>ST</Meins>
                    <Preis>7.0</Preis>
                    <Chargenpflicht/>
                </item>
                <item>
                    <Niederlassung>TKD</Niederlassung>
                    <Matnr>06182</Matnr>
                    <Maktx>Buchse / --806</Maktx>
                    <Meins>ST</Meins>
                    <Preis>24.7</Preis>
                    <Chargenpflicht/>
                </item>
            </TArtikel>
        </n0:ZPmUltimoArtikelResponse>
    </soap-env:Body>
</soap-env:Envelope>

输出XML:

<Object xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"
        Type="Article"
        Action="InsertOrSkip">
   <Property Name="Context" Value="1"/>
   <Property Name="Description" Value="Nadelhülse       HK 5520"/>
</Object>
<Object xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"
        Type="Article"
        Action="InsertOrSkip">
   <Property Name="Context" Value="1"/>
   <Property Name="Description" Value="Buchse / --806"/>
</Object>

注意::您可以在文档模板中使用根元素,否则它将不受欢迎。

EX:

<xsl:template match="/">
    <root>
        <xsl:apply-templates/>
    </root>
</xsl:template>