Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSLT重构XML节点_Xml_Xslt - Fatal编程技术网

使用XSLT重构XML节点

使用XSLT重构XML节点,xml,xslt,Xml,Xslt,希望使用XSLT转换我的XML。示例XML如下所示: <root> <info> <firstname>Bob</firstname> <lastname>Joe</lastname> </info> <notes> <note>text1</note> <note>text2</note> </notes>

希望使用XSLT转换我的XML。示例XML如下所示:

<root>
<info>
    <firstname>Bob</firstname>
    <lastname>Joe</lastname>
</info>
<notes>
    <note>text1</note>
    <note>text2</note>
</notes>
<othernotes>
    <note>text3</note>
    <note>text4</note>
</othernotes>
<root>
<info>
    <firstname>Bob</firstname>
    <lastname>Joe</lastname>
</info>
<notes>
    <note>text1</note>
    <note>text2</note>
    <note>text3</note>
    <note>text4</note>
</notes>
</root>

上下快速移动
乔
文本1
文本2
文本3
文本4

我希望提取所有“note”元素,并将它们放在父节点“notes”下

我想要的结果如下:

<root>
<info>
    <firstname>Bob</firstname>
    <lastname>Joe</lastname>
</info>
<notes>
    <note>text1</note>
    <note>text2</note>
</notes>
<othernotes>
    <note>text3</note>
    <note>text4</note>
</othernotes>
<root>
<info>
    <firstname>Bob</firstname>
    <lastname>Joe</lastname>
</info>
<notes>
    <note>text1</note>
    <note>text2</note>
    <note>text3</note>
    <note>text4</note>
</notes>
</root>

上下快速移动
乔
文本1
文本2
文本3
文本4
我尝试使用的XSLT允许我提取所有“注释”,但是,我不知道如何将它们包装回“注释”节点中

下面是我正在使用的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="notes|othernotes">
        <xsl:apply-templates select="note"/>
    </xsl:template>
    <xsl:template match="*">
    <xsl:copy><xsl:apply-templates/></xsl:copy>
    </xsl:template>

</xsl:stylesheet>

使用上述XSLT得到的结果是:

<root>
<info>
    <firstname>Bob</firstname>
    <lastname>Joe</lastname>
</info>
    <note>text1</note>
    <note>text2</note>
    <note>text3</note>
    <note>text4</note>
</root>

上下快速移动
乔
文本1
文本2
文本3
文本4

谢谢

您可以生成如下元素:

<xsl:element name="notes">
   <!-- inject content of notes element here using e.g. <xsl:copy> or <xsl:copy-of> -->
</xsl:element>

只要稍加修改,上述方法也可用于在特定XML命名空间中生成元素。 但是,由于您不希望在名称空间中生成元素,因此存在一个快捷方式:

<notes>
  <!-- inject content of notes element here using e.g. <xsl:copy> or <xsl:copy-of> -->
</notes>

在您的特定示例中,我将重新构造样式表以执行以下操作:

<xsl:template match="root">
   <root>
     <xsl:copy-of select="info"/>
     <notes>
        <xsl:copy-of select="*/note"/>
     </notes>
   </root>
</xsl:template>

您将寻找以下内容:-

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

<xsl:template match="/root">
  <xsl:copy>
     <xsl:apply-templates select="@*|node()[local-name() != 'notes' and local-name() != 'othernotes']
  </xsl:copy>
  <notes>
     <xsl:apply-templates select="othernotes/note | notes/note" />
  </notes>
</xsl:template>


select=“@*|node()[非(self::notes或self::othernotes)]”
;-)(虽然在这个例子中,
select=“info”
也会有同样的效果…@Tomalak:事实上我确实有这样的效果,但我现在想不出为什么@user268396的解决方案在非常具体的示例中起作用,我的假设是最低限度的(可能存在info的其他同级项,info可能包含不受此合并约束的注释元素等),这就是为什么我给解决方案加1的原因。我已经准备好了一个类似的,但是你更快了,所以我没有发布它又好又干净,+1。我想我会用它来代替重新创造,这是个人喜好的问题。此外,要在特定名称空间中创建元素,可以轻松声明前缀并使用它:
<代码>
仅在元素名称应为动态时才需要。