如何使用XSLT附加XML属性

时间:2012-06-28 21:56:17

标签: xslt

如果我的XML看起来像这样:

<Component Id="cmp25217AE65B163B199EDDA7F29198730A" Guid="{DEB29383-8BF1-4FD0-830B-DF8639F4069A}">
<Component Id="cmp93E1B1FFA5A62A43251E23BD65FBAA66" Guid="{76E8B8CE-835D-498E-9330-CE940C9510BF}">
<Component Id="cmp3D7B898C57056B0E87C3A964112BB9D6" Guid="{3BA9A892-C44F-4B2E-B0B9-B732120D35DB}">

我如何让XSLT将'Win64 =“yes”'附加到每个元素的末尾,如下所示:

<Component Id="cmp25217AE65B163B199EDDA7F29198730A" Guid="{DEB29383-8BF1-4FD0-830B-DF8639F4069A}" Win64="yes">
<Component Id="cmp93E1B1FFA5A62A43251E23BD65FBAA66" Guid="{76E8B8CE-835D-498E-9330-CE940C9510BF}" Win64="yes">
<Component Id="cmp3D7B898C57056B0E87C3A964112BB9D6" Guid="{3BA9A892-C44F-4B2E-B0B9-B732120D35DB}" Win64="yes">

3 个答案:

答案 0 :(得分:2)

您可以使用<xsl:attribute>元素并将其直接应用于<xsl:copy>元素(内部):

<xsl:template match="Component">
    <xsl:copy>
        <xsl:attribute name="Win64">yes</xsl:attribute>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

更新:嵌入在XSLT中,否则会复制Xml内容,如下所示:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Component">
        <xsl:copy>
            <xsl:attribute name="Win64">yes</xsl:attribute>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

更新2:这假设您只想将Win64="yes"添加到<Component>个元素。如果不是这种情况,则必须在插入附加属性的模板中调整match属性的XPath表达式。

更新3:格式正确的输入和输出文档:

我认为这是输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <Component Id="cmp25217AE65B163B199EDDA7F29198730A" Guid="{DEB29383-8BF1-4FD0-830B-DF8639F4069A}"/>
    <Component Id="cmp93E1B1FFA5A62A43251E23BD65FBAA66" Guid="{76E8B8CE-835D-498E-9330-CE940C9510BF}"/>
    <Component Id="cmp3D7B898C57056B0E87C3A964112BB9D6" Guid="{3BA9A892-C44F-4B2E-B0B9-B732120D35DB}"/>
</xml>

然后,上述XSLT的输出如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xml>
  <Component Win64="yes" Id="cmp25217AE65B163B199EDDA7F29198730A" Guid="{DEB29383-8BF1-4FD0-830B-DF8639F4069A}" />
  <Component Win64="yes" Id="cmp93E1B1FFA5A62A43251E23BD65FBAA66" Guid="{76E8B8CE-835D-498E-9330-CE940C9510BF}" />
  <Component Win64="yes" Id="cmp3D7B898C57056B0E87C3A964112BB9D6" Guid="{3BA9A892-C44F-4B2E-B0B9-B732120D35DB}" />
</xml>

答案 1 :(得分:0)

此样式表会将属性添加到您请求的所有元素中。请注意,属性的位置可能不在最后,但这无关紧要。

XML输入(格式正确)

<doc>
    <Component Id="cmp25217AE65B163B199EDDA7F29198730A" Guid="{DEB29383-8BF1-4FD0-830B-DF8639F4069A}"/>
    <Component Id="cmp93E1B1FFA5A62A43251E23BD65FBAA66" Guid="{76E8B8CE-835D-498E-9330-CE940C9510BF}"/>
    <Component Id="cmp3D7B898C57056B0E87C3A964112BB9D6" Guid="{3BA9A892-C44F-4B2E-B0B9-B732120D35DB}"/>
</doc>

XSLT 1.0 (使用Xalan和Saxon 6.5.5测试)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="text()|comment()|processing-instruction()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:attribute name="Win64">yes</xsl:attribute>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>     
    </xsl:template>

</xsl:stylesheet>

<强>输出

<doc Win64="yes">
   <Component Win64="yes" Id="cmp25217AE65B163B199EDDA7F29198730A" Guid="{DEB29383-8BF1-4FD0-830B-DF8639F4069A}"/>
   <Component Win64="yes" Id="cmp93E1B1FFA5A62A43251E23BD65FBAA66" Guid="{76E8B8CE-835D-498E-9330-CE940C9510BF}"/>
   <Component Win64="yes" Id="cmp3D7B898C57056B0E87C3A964112BB9D6" Guid="{3BA9A892-C44F-4B2E-B0B9-B732120D35DB}"/>
</doc>

答案 2 :(得分:-1)

很遗憾,您无法使用<xsl:copy>并添加更多属性。我是这样做的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/root">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="Component">
            <xsl:element name="Component">
                <xsl:attribute name="Id" select="@Id"/>
                <xsl:attribute name="Guid" select="@Guid"/>
                <xsl:attribute name="Win64" select="'yes'"/>
            </xsl:element>
    </xsl:template>
</xsl:stylesheet>