0

所以我有这个 XML

<?xml version="1.0" encoding="UTF-8"?>
<school name="AMU" location="CA">
   <student name="Jonny">
      <info age="20" class="A" />
   </student>
</school>

我想做的是只创建和之间的schoolstudent

<?xml version="1.0" encoding="UTF-8"?>
<school name="AMU" location="CA">
   <table>
    .....
   <table />
   <student name="Jonny">
      <info age="20" class="A" />
   </student>
</school>

我知道如何使用 foreach-groupconcat它创建一个表。

XSL 我正在研究,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
   <xsl:template match="school">
      <xsl:element name="school">
         <xsl:attribute name="name">
            <xsl:value-of select="@name" />
         </xsl:attribute>
         <xsl:attribute name="location">
            <xsl:value-of select="@location" />
         </xsl:attribute>
         <table>
            created table
         </table>
            <xsl:copy>
               <xsl:apply-templates select="student" />
            </xsl:copy>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

我的问题是我想复制我的表下的所有节点,所以我的输出 XML 将在和之间有表,school并且student没有其他任何改变。(表工作正常)

我应该怎么办?

谢谢。

4

1 回答 1

0

首先,您的代码不必要地冗长。这个:

<xsl:element name="school">
         <xsl:attribute name="name">
            <xsl:value-of select="@name" />
         </xsl:attribute>
         <xsl:attribute name="location">
            <xsl:value-of select="@location" />
         </xsl:attribute>

可以用这个代替:

<school name="{@name}" location="{@location}">

或者,如果您愿意,可以通过

<xsl:copy>
   <xsl:copy-of select="@*"/>

那么关于你的问题,你可以替换这个:

   <xsl:copy>
       <xsl:apply-templates select="student" />
   </xsl:copy>

这样:

<xsl:copy-of select="student"/>
于 2013-11-06T08:12:05.913 回答