【问题标题】:Xslt returns empty xmlXslt 返回空的 xml
【发布时间】:2018-10-29 11:11:02
【问题描述】:

我正在尝试使用 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>

希望大家帮帮我

【问题讨论】:

标签: xml xslt


【解决方案1】:

在文档节点模板中使用apply-templates 像这样&lt;xsl:template match="/"&gt;&lt;xsl:apply-templates/&gt;&lt;/xsl:template&gt;

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>

注意:您可以在文档模板中使用根元素,否则将无法使用。

前:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多