我需要使用XSLT从下面给出XML删除父节点帮助..
<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="OpenProblems2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="OpenProblems2" Name="OpenProblems2">
<Hello>
<NewDataSet>
<Table>
<a>1832874</a>
<b>HUME-9063</b>
<c>not informed</c>
</Table>
<Table>
<a>1832874</a>
<b>HUME-9063</b>
<c>not informed</c>
</Table>
</NewDataSet>
</Hello>
</Report>
输出应该看起来像 -
<NewDataSet>
<Table>
<a>1832874</a>
<b>HUME-9063</b>
<c>not informed</c>
</Table>
<Table>
<a>1832874</a>
<b>HUME-9063</b>
<c>not informed</c>
</Table>
</NewDataSet>
XSLT应该删除报告,你好&NewDataset元素。 请...您的帮助将不胜感激。
标准的方法使用XSLT被定义身份模板 ,使小的变化到一个XML文件,其拷贝一切从输入到输出,是除非有更具体的模板覆盖:
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
然后提供特定的模板来匹配你想改变的事情。 在如果你知道总会有只有一个第三级元素(NewDataSet)这种情况下,那么你可以使用跳过外包裹件的前两层
<xsl:template match="/">
<xsl:apply-templates select="*/*/*" />
</xsl:template>
这两个模板一起将产生出这样的
<NewDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="OpenProblems2">
<Table>
<a>1832874</a>
<b>HUME-9063</b>
<c>not informed</c>
</Table>
<Table>
<a>1832874</a>
<b>HUME-9063</b>
<c>not informed</c>
</Table>
</NewDataSet>
如果您也想删除所有命名空间,那么你需要添加第三个模板是这样的:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
采取任何元素的所有(或没有)命名空间和与相同的本地名称的新元素,但不是在一个命名空间代替它。
如果你想离开的命名空间,因为它是,那么你需要的是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:o="OpenProblems2">
<xsl:template match="/">
<xsl:copy-of select="o:NewDataSet"/>
</xsl:template>
</xsl:stylesheet>
这种要求是最好用的身份模板处理。 身份模板将允许您通过您的大部分XML不变,然后只处理是必要的碎片。 一个简单的身份是这样的:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
这符合所有属性,注释,元素等,并将它们复制到输出。 任何更具体的比赛将优先考虑。
您的样本输出实际上并没有删除NewDataSet
所以我也没有元素。 如果你想删除它,将它添加到下面的模板(但请记住,它会让你的输出形成不良)
<xsl:template match="Hello|Report">
<xsl:apply-templates/>
</xsl:template>
这个模板同时匹配Hello
和Report
内容,并通过简单的应用模板,以他们的孩子不复制实际的节点向输出处理它们。
因此,一个样式表,例如:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns="OpenProblems2">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Hello|Report">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
将您的样品输入并生成输出样本。 作为@伊恩 - 罗伯茨指出,如果你真的想去掉你需要处理,太多的命名空间。