特定子元素上的XML转换集属性
我是XML新手,到目前为止,我已经设法在阅读一些东西和寻找其他人问题的答案时混过去了,但我被卡住了 在下面的示例XML中,我需要将特定子元素上的XML转换集属性,xml,xslt,attributes,Xml,Xslt,Attributes,我是XML新手,到目前为止,我已经设法在阅读一些东西和寻找其他人问题的答案时混过去了,但我被卡住了 在下面的示例XML中,我需要将一些有趣的信息转换为一些有趣的信息。 Value是可变的,因此正确的Value元素是Special Description的同级元素,它是常量 <job> <job_type>job</job_type> <third_party_id>AA123456-AA</third_party_id>
一些有趣的信息
转换为一些有趣的信息
。
Value是可变的,因此正确的Value元素是Special Description
的同级元素,它是常量
<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>
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>
3.
3.
排列
2.
2.
非常感谢您的帮助。如果您只想修改特定的
值
元素,请使用:
XSLT1.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的建议 然而,使用@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>
3.
3.
生成完整的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>
3.
3.
排列
2.
2.
转换后的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>
工作
AA123456-AA
承诺日期
只读
2020-01-01T00:00:00Z
工单
只读
L1
特别说明
只读
一些有趣的信息
谢谢,这让我达到了目的,使用上面匹配的元素,但在转换后的XML中,我得到了一些有趣的信息。将您的匹配与原始转换中的
和
相结合,得到了我想要的结果一些有趣的信息
,我认为您弄错了-但是您显然在做一些超出您问题范围的事情。很抱歉澄清,当我完全按照您提供的方式使用XSLT时,不会出现这种情况,但当我将
添加到XSLT时,它会出现。这一定与“xmlns:soap=”有关“在我的XLST开始的时候,我想我实际上可以删除它,这是我从我开始编写转换时遇到的一个示例中得到的。如果你不需要它,那么你肯定应该删除它。当然,您不需要使用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>
<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>
<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>