XML:
<t>
<ScreenSize>
<Width>1440</Width>
<Height>900</Height>
</ScreenSize>
<ConfigurationHotSpots>
<Rectangle>
<Location>
<X>0</X>
<Y>0</Y>
</Location>
<Size>
<Width>50</Width>
<Height>50</Height>
</Size>
<X>0</X>
<Y>0</Y>
<Width>50</Width>
<Height>50</Height>
</Rectangle>
</ConfigurationHotSpots>
</t>
所需的输出 XML:
<t>
<ScreenSizeWidth>1440</ScreenSizeWidth>
<ScreenSizeWidth>900</ScreenSizeWidth>
<ConfigurationHotSpotsRectangleLocationX>0</ConfigurationHotSpotsRectangleLocationX>
<ConfigurationHotSpotsRectangleLocationY>0</ConfigurationHotSpotsRectangleLocationY>
<ConfigurationHotSpotsRectangleSizeWidth>50</ConfigurationHotSpotsRectangleSizeWidth>
<ConfigurationHotSpotsRectangleSizeHeight>50</ConfigurationHotSpotsRectangleSizeHeight>
<ConfigurationHotSpotsRectangleX>0</ConfigurationHotSpotsRectangleX>
<ConfigurationHotSpotsRectangleY>0</ConfigurationHotSpotsRectangleY>
<ConfigurationHotSpotsRectangleWidth>50</ConfigurationHotSpotsRectangleWidth>
<ConfigurationHotSpotsRectangleHeight>50</ConfigurationHotSpotsRectangleHeight>
</t>
Rules:
- 对于定义的节点集中的每个元素(在本例中
<ScreenSize> | <ConfigurationHotSpots>
),执行以下操作:处理所有叶子后代(即没有子元素的叶子),以便创建新元素;这个新元素的名称应该是当前节点和无子节点之间的所有元素的串联。
- 整个文档中这些“块”的数量是可变的,因此没有手动模板(即仅处理
<ScreenSize>
,仅处理的后代<ConfigurationHotSpots>
, etc.)
我目前拥有的:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="ScreenSize|ConfigurationHotSpots">
<xsl:apply-templates select="descendant::*[not(*)]" mode="descendants" />
</xsl:template>
<xsl:template match="*" mode="descendants">
<xsl:element name="{concat(name(ancestor::*[not(self::t)]), name())}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
问题似乎是name(ancestor::*[not(self::t)])
部分;它没有做我希望它做的事情(神奇地一个接一个地输出这些元素的名称)。相反,这就是我得到的:
<?xml version="1.0" encoding="UTF-8"?>
<t>
<ScreenSizeWidth>1440</ScreenSizeWidth>
<ScreenSizeHeight>900</ScreenSizeHeight>
<ConfigurationHotSpotsX>0</ConfigurationHotSpotsX>
<ConfigurationHotSpotsY>0</ConfigurationHotSpotsY>
<ConfigurationHotSpotsWidth>50</ConfigurationHotSpotsWidth>
<ConfigurationHotSpotsHeight>50</ConfigurationHotSpotsHeight>
<ConfigurationHotSpotsX>0</ConfigurationHotSpotsX>
<ConfigurationHotSpotsY>0</ConfigurationHotSpotsY>
<ConfigurationHotSpotsWidth>50</ConfigurationHotSpotsWidth>
<ConfigurationHotSpotsHeight>50</ConfigurationHotSpotsHeight>
</t>
提前致谢!
Doing name(ancestor::*[not(self::t)])
不会返回名称列表,而只返回最后一个匹配的名称(或者是第一个?)。
您可以采取稍微不同的方法,与您当前正在做的事情相差不远,不是直接跳到“叶子”元素,而是依次匹配每个级别,而是保持传递的元素名称的连续串联通过参数从一个级别到另一个级别。
试试这个 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="ScreenSize|ConfigurationHotSpots">
<xsl:apply-templates mode="descendants">
<xsl:with-param name="name" select="local-name()" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="descendants">
<xsl:param name="name" />
<xsl:apply-templates mode="descendants">
<xsl:with-param name="name" select="concat($name, local-name())" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*[not(*)]" mode="descendants">
<xsl:param name="name" />
<xsl:element name="{concat($name, local-name())}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当应用于示例 XML 时,输出如下
<t>
<ScreenSizeWidth>1440</ScreenSizeWidth>
<ScreenSizeHeight>900</ScreenSizeHeight>
<ConfigurationHotSpotsRectangleLocationX>0</ConfigurationHotSpotsRectangleLocationX>
<ConfigurationHotSpotsRectangleLocationY>0</ConfigurationHotSpotsRectangleLocationY>
<ConfigurationHotSpotsRectangleSizeWidth>50</ConfigurationHotSpotsRectangleSizeWidth>
<ConfigurationHotSpotsRectangleSizeHeight>50</ConfigurationHotSpotsRectangleSizeHeight>
<ConfigurationHotSpotsRectangleX>0</ConfigurationHotSpotsRectangleX>
<ConfigurationHotSpotsRectangleY>0</ConfigurationHotSpotsRectangleY>
<ConfigurationHotSpotsRectangleWidth>50</ConfigurationHotSpotsRectangleWidth>
<ConfigurationHotSpotsRectangleHeight>50</ConfigurationHotSpotsRectangleHeight>
</t>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)