XSLT获取多个属性中单个属性的值到相同的字段值
XSLT to Get value of a single attribute among multiple attributes to same field value
我有一个xml
<PiecesTransportationQuantity unitCode="EA" text="1.0"/>
需要将“文本”属性的值转换为相同的字段值,属性 unitCode 应保留为属性:
<PiecesTransportationQuantity unitCode="EA"> 1.0 </PiecesTransportationQuantity>
请求为同一 .
提供 xslt
我用过
<xsl:template match="PiecesTransportationQuantity/@unitCode/@text">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
你可以这样做:
<xsl:template match="PiecesTransportationQuantity">
<xsl:copy>
<xsl:attribute name="unitCode" select="@unitCode"/>
<xsl:value-of select="@text"/>
</xsl:copy>
</xsl:template>
如果您的样式表也有 identity transform 模板,您可以简单地做:
<xsl:template match="PiecesTransportationQuantity/@text">
<xsl:value-of select="."/>
</xsl:template>
否则你需要做类似的事情:
<xsl:template match="PiecesTransportationQuantity">
<xsl:copy>
<xsl:copy-of select="@unitCode"/>
<xsl:value-of select="@text"/>
</xsl:copy>
</xsl:template>
模板中使用的模式(同时使用两个属性)匹配将不匹配任何内容。
如果您想使用您的代码模式,请尝试:
<xsl:template match="PiecesTransportationQuantity">
<xsl:element name="{name()}">
<xsl:copy-of select="@* except @text"/>
<xsl:value-of select="@text"/>
</xsl:element>
</xsl:template>
否则您可以使用@michael.hor257k 的回答中的第二个建议:
我有一个xml
<PiecesTransportationQuantity unitCode="EA" text="1.0"/>
需要将“文本”属性的值转换为相同的字段值,属性 unitCode 应保留为属性:
<PiecesTransportationQuantity unitCode="EA"> 1.0 </PiecesTransportationQuantity>
请求为同一 .
提供 xslt我用过
<xsl:template match="PiecesTransportationQuantity/@unitCode/@text">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
你可以这样做:
<xsl:template match="PiecesTransportationQuantity">
<xsl:copy>
<xsl:attribute name="unitCode" select="@unitCode"/>
<xsl:value-of select="@text"/>
</xsl:copy>
</xsl:template>
如果您的样式表也有 identity transform 模板,您可以简单地做:
<xsl:template match="PiecesTransportationQuantity/@text">
<xsl:value-of select="."/>
</xsl:template>
否则你需要做类似的事情:
<xsl:template match="PiecesTransportationQuantity">
<xsl:copy>
<xsl:copy-of select="@unitCode"/>
<xsl:value-of select="@text"/>
</xsl:copy>
</xsl:template>
模板中使用的模式(同时使用两个属性)匹配将不匹配任何内容。
如果您想使用您的代码模式,请尝试:
<xsl:template match="PiecesTransportationQuantity">
<xsl:element name="{name()}">
<xsl:copy-of select="@* except @text"/>
<xsl:value-of select="@text"/>
</xsl:element>
</xsl:template>
否则您可以使用@michael.hor257k 的回答中的第二个建议: