简体   繁体   English

如何使用 XSLT 编辑 XML 文件中的元素?

[英]How to I use XSLT to edit elements in my XML file?

I have an XML file here:我在这里有一个 XML 文件:

<?xml version="1.0" encoding="utf-16"?>
<class>
    <student>
        <name>Ben</name>
        <age>1</age>
        <ages>4</ages>
        <entryprofile></entryprofile>
        <node>1</node>
    </student>
    <student>
        <name>Steve</name>
        <age>2</age>
        <ages>3</ages>
        <entryprofile></entryprofile>
        <node>1</node>
    </student>
</class>

I am trying to promote the entryprofile element so that it becomes a child element of student , rather than an attribute of student --and I want it to contain node .我正在尝试提升entryprofile元素,使其成为student的子元素,而不是student的属性——我希望它包含node I have tried to apply the following XSL in order to do this:为了做到这一点,我尝试应用以下 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="student">
  <xsl:copy-of select="name"/>
  <new-level>
     <xsl:copy-of select="entryprofile"/>
  </new-level>
</xsl:template>
</xsl:stylesheet>

This doesn't seem to be doing much apart from this:这似乎并没有做太多除了这个:

<?xml version="1.0" encoding="utf-16"?>
    <name>Ben</name><new-level><entryprofile>g</entryprofile></new-level>
    <name>Steve</name><new-level><entryprofile>g</entryprofile></new-level>

But what I am looking for is this:但我正在寻找的是:

<?xml version="1.0" encoding="utf-16"?>
<class>
    <student>
        <name>Ben</name>
        <age>1</age>
        <ages>4</ages>
        <entryprofile>
          <node>1</node>
        </entryprofile>
    </student>
    <student>
        <name>Steve</name>
        <age>2</age>
        <ages>3</ages>
        <entryprofile>
          <node>1</node>
        </entryprofile>
    </student>
</class>

You can see there that entryprofile for both stdudents has become a child on account of node becoming a child of entryprofile .您可以在那里看到,由于node成为 entryprofile 的孩子,因此两个entryprofileentryprofile都已成为孩子。

Would anyone know where I am going wrong, and what I can do to achieve my desired result?有谁知道我哪里出错了,我能做些什么来达到我想要的结果? Many thanks.非常感谢。

Try this XSLT:试试这个 XSLT:

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

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

    <xsl:template match="entryprofile">
        <xsl:copy>
            <xsl:copy-of select="../node"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node"/>
</xsl:stylesheet>

Here are two alternatives that accomplish the same thing in slightly different ways:以下是两种以略微不同的方式完成相同任务的替代方案:

This one is the correct version of what you tried to do:这是您尝试做的正确版本:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="student">
    <xsl:copy>
        <xsl:copy-of select="name | age | ages | profile"/>
        <entryprofile>
            <xsl:copy-of select="node"/>
        </entryprofile>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This one is a shorter version of the above:这是上述版本的较短版本:

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

<xsl:template match="/class">
    <xsl:copy>
        <xsl:for-each select="student">
            <xsl:copy>
                <xsl:copy-of select="name | age | ages | profile"/>
                <entryprofile>
                    <xsl:copy-of select="node"/>
                </entryprofile>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM