Xml 使用xslt在标记上设置条件?
XSL新手 我有一个xml文件Xml 使用xslt在标记上设置条件?,xml,xslt,Xml,Xslt,XSL新手 我有一个xml文件 <parent> <child1>The child</child1> <child2> <subchild1>The subchild 1 </subchild1> <subchild2>The subchild 2 </subchild2> <ref>1</ref> </child2>
<parent>
<child1>The child</child1>
<child2>
<subchild1>The subchild 1 </subchild1>
<subchild2>The subchild 2 </subchild2>
<ref>1</ref>
</child2>
<child3>
<address> 23 </address>
<mail> test@test.com </mail>
</child3>
</parent>
孩子
亚儿童1
亚儿童2
1.
23
test@test.com
我希望xsl做出以下更改
<parent>
<child1>The child</child>
<child2>
<subchild1>The subchild 1 </subchild>
<subchild2>The subchild 2 </subchild>
<ref refid = "aff1">1</ref>
</child2>
<child3>
<address> 23 </address>
<mail type="email"> test@test.com </mail>
</parent>
孩子
亚儿童1
亚儿童2
1.
23
test@test.com
到目前为止,我的XSL是
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
我可以迭代每个标记和文本,但是如何放置if语句并进行更改。
aff
将被固定,但数字将被追加。
我在ubuntu中运行它的方式是
$xsltproc iterate1.xsl headerout1.xml
通过使用标识模板,您已经有了良好的开端。现在需要做的就是添加与要更改的节点匹配的模板。XSLT具有模板优先级的概念,因此如果两个模板与给定节点匹配,则使用优先级较高的模板。(与优先级为-0.5的匹配“node()”的模板相比,匹配特定节点名称(如“ref”)的模板的优先级为0)
因此,要转换ref
,您需要这样做
<xsl:template match="ref">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
对于
(要使XML格式良好,实际上应该是
),refid
属性的值来自哪里?它总是“1”吗?它是否与
节点的值相同?或者,对于文档中每次出现的
,是否要递增?ThanksIt将与ref
元素内容相同
<xsl:template match="ref">
<ref id="{.}">
<xsl:apply-templates select="@*|node()"/>
</ref>
</xsl:template>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="mail">
<mail type="email">
<xsl:apply-templates select="@*|node()"/>
</mail>
</xsl:template>
<xsl:template match="ref">
<ref id="{.}">
<xsl:apply-templates select="@*|node()"/>
</ref>
</xsl:template>
</xsl:transform>