在创建 XSLT 时需要帮助,我有源 XML 和目标 XML [英] Need help in creating XSLT, i do have Source and Target XML
问题描述
我正在尝试为源 XML 编写 XSLT 文档,我也有 Traget XML(它应该是什么样子)
I am trying to write XSLT document for the source XML and i do have Traget XML too (what it should look like)
我的来源看起来像:
<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)
What i want to achieve is (Target XML)
<d>
<e ID="1" />
<e ID="2" />
<e ID="3" />
</d>
我一直在尝试编写我的 XSLT,但无法让它工作.我一直在使用一些在线工具,在那里我提供了我的源代码并编写了 XSLT,但我没有得到任何结果.(从未在 XSLT 中工作过)
I have been trying to write my XSLT but couldn't get it working. I have been using some online tools, where i give my source and write XSLT but i am not getting any result. (never worked in XSLT)
有人可以帮我写这篇文章或指出我的写作方向.
Can someone please help me in writing this or point me in write direction.
我尝试过的是:
<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>
谢谢
推荐答案
您需要为源文档使用的每个命名空间分配一个前缀,并在对源文档中的元素进行寻址时使用适当的前缀.这是对您的样式表的更正:
You need to assign a prefix to each namespace used by the source document, and use the appropriate prefix when addressing elements in the source document. Here's a correction of your stylesheet:
<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>
当然,您可以将其简化为:
Of course, you could simplify this to:
<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>
要实现所需的输出,请更改:
To achieve the required output, change:
<e>
<xsl:value-of select="@MemberID"/>
</e>
到:
<e ID="{@MemberID}"/>
这篇关于在创建 XSLT 时需要帮助,我有源 XML 和目标 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!