Is it possible to change given XML structure
是否有可能改变给定的XML结构
<Cars>
<Car>Honda</Car>
<Car>Ferrari</Car>
</Cars>
with XLST to
与的XLST
<Cars>
<Honda></Honda>
<Ferrari></Ferrari>
</Cars>
I know XSLT a little, but I'm not sure how to create variable tags.
我对XSLT略知一二,但不知道如何创建变量标记。
Thanks everybody. I appreciate all three answers and have up voted them.
谢谢大家。我很欣赏这三个答案,并投了赞成票。
3 个解决方案
#1
2
Try this:
试试这个:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Cars">
<Cars>
<xsl:for-each select="Car">
<xsl:element name="{.}"/>
</xsl:for-each>
</Cars>
</xsl:template>
</xsl:stylesheet>
#2
3
You could use <xsl:element> to create elements by a given name. E.g. in your case it will be something like:
您可以使用
<xsl:template match="Car">
<xsl:element name="{text()}"></xsl:element>
</xsl:template>
UPD: This is a fragment. Usually it is a good approach for such transformations which intended just to modify a few nodes in the tree. You define the copy-template:
这是一个碎片。通常,这是一种很好的转换方法,只需要修改树中的几个节点。你定义copy-template:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
which just copies the whole xml tree as is and then add some customised templates for particular elements, attributes, etc, e.g. as for the "Car" above.
它只是按原样复制整个xml树,然后为特定的元素、属性等添加一些定制模板,例如上面的“Car”。
#3
3
You're looking for xsl:element
, with a name computed at run-time using curly braces like so:
您正在寻找xsl:element,它在运行时使用大括号计算名称,如下所示:
<xsl:template match="/">
<xsl:for-each select="Cars/Car">
<xsl:element name="{.}"/>
</xsl:for-each>
</xsl:template>
#1
2
Try this:
试试这个:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Cars">
<Cars>
<xsl:for-each select="Car">
<xsl:element name="{.}"/>
</xsl:for-each>
</Cars>
</xsl:template>
</xsl:stylesheet>
#2
3
You could use <xsl:element> to create elements by a given name. E.g. in your case it will be something like:
您可以使用
<xsl:template match="Car">
<xsl:element name="{text()}"></xsl:element>
</xsl:template>
UPD: This is a fragment. Usually it is a good approach for such transformations which intended just to modify a few nodes in the tree. You define the copy-template:
这是一个碎片。通常,这是一种很好的转换方法,只需要修改树中的几个节点。你定义copy-template:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
which just copies the whole xml tree as is and then add some customised templates for particular elements, attributes, etc, e.g. as for the "Car" above.
它只是按原样复制整个xml树,然后为特定的元素、属性等添加一些定制模板,例如上面的“Car”。
#3
3
You're looking for xsl:element
, with a name computed at run-time using curly braces like so:
您正在寻找xsl:element,它在运行时使用大括号计算名称,如下所示:
<xsl:template match="/">
<xsl:for-each select="Cars/Car">
<xsl:element name="{.}"/>
</xsl:for-each>
</xsl:template>