需要创建XSLT帮助,我有源和目标XML需要创建XSLT帮助,我有源和目标XML(Need help

2019-05-12 12:10发布

我想写XSLT文档的源XML和我确实有追求的目标XML太(它应该是什么样子)

我的资料来源是这样的:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body>
<a xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
  <b>
    <c>
      <d>
        <e MemberID="1" />
        <e MemberID="2" />
        <e MemberID="3" />
       </d>
    </c>
  </b>
</a>
</soap:Body>
</soap:Envelope>

我想要实现的是(目标XML)

<d>
   <e ID="1" />
   <e ID="2" />
   <e ID="3" />
</d>

我一直在努力写我的XSLT,但不能得到它的工作。 我一直在使用一些在线工具,在这里我给我的源代码和写XSLT,但我没有得到任何结果。 (在XSLT从来没有工作过)

有人可以帮我写这篇或指向我写的方向。

我曾尝试是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:apply-templates select="a/b/c/d"/>
  </xsl:template>
  <xsl:template match="d">
<d>
  <xsl:for-each select="e">
    <e>
          <xsl:value-of select="@MemberID"/> -- I know its wrong, but just want something to work                  
    </e>
  </xsl:for-each>
</d>
  </xsl:template>
</xsl:stylesheet>

谢谢

Answer 1:

你需要指定一个前缀,由源文件使用的每个命名空间,解决源文件中的元素时,使用适当的前缀。 这是你的样式表的修正:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:dir="http://schemas.microsoft.com/sharepoint/soap/directory/"
exclude-result-prefixes="soap dir">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <xsl:apply-templates select="soap:Envelope/soap:Body/dir:a/dir:b/dir:c/dir:d"/>
</xsl:template> 

<xsl:template match="dir:d">    
    <d>
        <xsl:for-each select="dir:e">
            <e>
                <xsl:value-of select="@MemberID"/>                 
            </e>
        </xsl:for-each>
    </d>
</xsl:template>

</xsl:stylesheet>

这将产生以下结果:

<?xml version="1.0" encoding="utf-8"?>
<d>
  <e>1</e>
  <e>2</e>
  <e>3</e>
</d>

当然,你可以简化这个来:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:dir="http://schemas.microsoft.com/sharepoint/soap/directory/"
exclude-result-prefixes="soap dir">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <d>
        <xsl:for-each select="soap:Envelope/soap:Body/dir:a/dir:b/dir:c/dir:d/dir:e">
            <e>
                <xsl:value-of select="@MemberID"/>                 
            </e>
        </xsl:for-each>
    </d>
</xsl:template> 

</xsl:stylesheet>

为了实现所需的输出,更改:

            <e>
                <xsl:value-of select="@MemberID"/>                 
            </e>

至:

            <e ID="{@MemberID}"/>


文章来源: Need help in creating XSLT, i do have Source and Target XML
标签: xml xslt