[英]XSL copy then replace attributes
INPUT.xml INPUT.xml
<human gender="male" nationality="american">
<property>blank</property>
</human>
(desired) OUTPUT.xml (所需)OUTPUT.xml
<human gender="male" nationality="american">
<property>blank</property>
</human>
<person gender="female" nationality="british">
<property>blank</property>
</person>
Hi guys, the above is my desired transform. 大家好,以上是我想要的转换。 I have the following xsl so far:
到目前为止,我有以下xsl:
<xsl:template match="human">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
<person>
<xsl:apply-templates select="node()|@*"/>
</person>
</xsl:template>
But how i go about replacing the attribute values I tried using a xsl:choose but without luck 但是我该如何替换使用xsl:choose但没有运气的属性值
Your stylesheet was close, it just lacks a template matching those nodes that are not matched by your other template, so they do not get picked up by the built-in templates of XSLT . 您的样式表很接近,它缺少与其他模板不匹配的那些节点匹配的模板,因此XSLT的内置模板不会拾取它们。
For the transformation of the attributes I chose to introduce modes, so that some templates only match in the second case where you want to change the attribute values. 对于属性的转换,我选择引入模式,以便某些模板仅在要更改属性值的第二种情况下才匹配。
The following stylesheet 以下样式表
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="human">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
<person>
<!-- mode is used to separate this case from the other where things are copied unchanged -->
<xsl:apply-templates select="node()|@*" mode="other" />
</person>
</xsl:template>
<!-- templates for normal mode -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- templates for "other" mode -->
<xsl:template match="@gender" mode="other">
<xsl:attribute name="gender">
<xsl:if test=". = 'male'">female</xsl:if>
<xsl:if test=". = 'female'">male</xsl:if>
</xsl:attribute>
</xsl:template>
<xsl:template match="@nationality" mode="other">
<xsl:attribute name="nationality">
<xsl:if test=". = 'american'">british</xsl:if>
<xsl:if test=". = 'british'">american</xsl:if>
</xsl:attribute>
</xsl:template>
<xsl:template match="node()|@*" mode="other">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when applied to this input: 应用于此输入时:
<human gender="male" nationality="american">
<property>blank</property>
</human>
gives the following result: 给出以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<human gender="male" nationality="american">
<property>blank</property>
</human>
<person gender="female" nationality="british">
<property>blank</property>
</person>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.