XML 转换特定子元素的集属性

XML Transform set attribute on specific child element

我是 XML 的新手,到目前为止,我已经设法糊里糊涂地阅读了一些东西并查看了其他人问题的答案,但我被卡住了。

在下面的示例 XML 中,我需要将 <value>some interesting information</value> 转换为 <value web_label="3" mobile_label="3">some interesting information</value>。 值是可变的,因此正确的值元素是 <name>Special Description</name> 的同级元素,它是常量。

<job>
    <job_type>job</job_type>
    <third_party_id>AA123456-AA</third_party_id>
    <custom_fields>
        <custom_field>
            <name>Date Promised</name>
            <permission>read_only</permission>
            <value>2020-01-01T00:00:00Z</value>
        </custom_field>
            <custom_field>
            <name>Work Order</name>
            <permission>read_only</permission>
            <value>L1</value>
        </custom_field>
        <custom_field>
            <name>Special Description</name>
            <permission>read_only</permission>
            <value>some interesting information</value>
        </custom_field>
    </custom_fields>
</job>

在我的反复试验中,我成功地使用以下方法设置了所有值元素的属性。 我想我可以用 match="/job/custom_fields/custom_field[name=Special Description]/value" 替换 match="/job/custom_fields/custom_field/value 到 select 特定元素但是错误。

有没有我可以用来实现的 xpath 查询this/what 是在我的转换中执行此操作的最佳方法?

    <xsl:template match="/job/custom_fields/custom_field/value">
        <xsl:copy>
            <xsl:attribute name="web_label">3</xsl:attribute>
            <xsl:attribute name="mobile_label">3</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                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="/job/custom_fields/custom_field[name=Special Description]/value">
        <xsl:copy>
            <xsl:attribute name="web_label">3</xsl:attribute>
            <xsl:attribute name="mobile_label">3</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="custom_fields">
        <xsl:copy>
            <xsl:attribute name="type">array</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="third_party_id">
        <xsl:copy>
            <xsl:attribute name="web_label">2</xsl:attribute>
            <xsl:attribute name="mobile_label">2</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

感谢您的帮助。

如果您只想修改特定的 value 元素,请使用 predicate:

匹配它

XSLT 1.0

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

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

<xsl:template match="custom_field[name='Special Description']/value">
    <value web_label="3" mobile_label="3">
        <xsl:value-of select="."/>  
    </value>
</xsl:template>

</xsl:stylesheet>

感谢@michael.hor257 的建议

<xsl:template match="custom_field[name='Special Description']/value">
   <value web_label="3" mobile_label="3">
       <xsl:value-of select="."/>  
   </value>
</xsl:template>

出于某种原因,在我现有的 XSLT 中使用时,我在转换后的 XML.

中得到了 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/
<job>
    <job_type>job</job_type>
    <third_party_id web_label="2" mobile_label="2">AA123456-AA</third_party_id>
    <custom_fields type="array">
        <custom_field>
            <name>Date Promised</name>
            <permission>read_only</permission>
            <value>2020-01-01T00:00:00Z</value>
        </custom_field>
            <custom_field>
            <name>Work Order</name>
            <permission>read_only</permission>
            <value>L1</value>
        </custom_field>
        <custom_field>
            <name>Special Description</name>
            <permission>read_only</permission>
            <value xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                web_label="3"
                mobile_label="3">some interesting information</value>
        </custom_field>
    </custom_fields>
</job>

将@michael.hor257 的 xpath 查询与我原来的 XSLT 中的 xsl:copy 和 xsl:attribute 结合使用,但是成功了,所以我得到了:

    <xsl:template match="custom_field[name='Special Description']/value">
        <xsl:copy>
            <xsl:attribute name="web_label">3</xsl:attribute>
            <xsl:attribute name="mobile_label">3</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

生成完整的 XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                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="custom_field[name='Special Description']/value">
        <xsl:copy>
            <xsl:attribute name="web_label">3</xsl:attribute>
            <xsl:attribute name="mobile_label">3</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="custom_fields">
        <xsl:copy>
            <xsl:attribute name="type">array</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="third_party_id">
        <xsl:copy>
            <xsl:attribute name="web_label">2</xsl:attribute>
            <xsl:attribute name="mobile_label">2</xsl:attribute>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

转换后的 XML 看起来像这样:

<job>
    <job_type>job</job_type>
    <third_party_id web_label="2" mobile_label="2">AA123456-AA</third_party_id>
    <custom_fields type="array">
        <custom_field>
            <name>Date Promised</name>
            <permission>read_only</permission>
            <value>2020-01-01T00:00:00Z</value>
        </custom_field>
            <custom_field>
            <name>Work Order</name>
            <permission>read_only</permission>
            <value>L1</value>
        </custom_field>
        <custom_field>
            <name>Special Description</name>
            <permission>read_only</permission>
            <value web_label="3" mobile_label="3">some interesting information</value>
        </custom_field>
    </custom_fields>
</job>