如何使用 xslt 在具有相同名称的第二个和第三个 xml 元素中显示文本



我想知道我是否可以通过使用xslt的xml部分的值来显示名称。如果这是不可能的,那么我将如何使用 xslt 简单地显示名称?我还想知道 xslt 可以更改 xml 中的元素名称吗?

我的 xml 是这个

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="myxmltest.xsl type="text/xsl" version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ?>
<x>
<y>
<z value="mike"></z>
<z value="john"></z>
<z value="dave"></z>
</y>
</x>

我的xsl是这个

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="z">
<xsl:apply-templates select="z[1]" />mike
</xsl:template>
<xsl:template match="z">
<xsl:apply-templates select="z[2]" />john
</xsl:template>
<xsl:template match="z">
<xsl:apply-templates select="z[3]" />dave
</xsl:template>
</xsl:stylesheet>

xml 中的所需结果为:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="myxmltest.xsl type="text/xsl" version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ?>
<boss>
<manager>
<employee value="mike">mike</employee>
<employee value="john">john</employee>
<employee value="dave">dave</employee>
</manager>
</boss>

您显示的结果可以通过以下方式轻松完成:

XSLT 1.0

<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="*"/>
<xsl:template match="x">
<boss>
<xsl:apply-templates/>
</boss>
</xsl:template>
<xsl:template match="y">
<manager>
<xsl:apply-templates/>
</manager>
</xsl:template>
<xsl:template match="z">
<employee value="{@value}">
<xsl:value-of select="@value"/>
</employee>
</xsl:template>
</xsl:stylesheet>

最新更新