XSLT 1.0 向多个匹配项添加元素
XSLT 1.0 Adding element to multple matches
我是 XSLT 的新手。
我正在尝试向从 API 接收到的多个对象添加元素。
所以这是从 API 收到的示例 XML:
<array xmlns="urn:com.sap.b1i.bizprocessor:bizatoms" name="orders">
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<bunch of info here 1st order>
</object>
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<bunch of info here 2nd order>
</object>
....
</array>
输入是这样的,我需要这样的输出:
<array xmlns="urn:com.sap.b1i.bizprocessor:bizatoms" name="orders">
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<Location>USA</Location>
<bunch of info here 1st order>
</object>
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<Location>USA</Location>
<bunch of info here 2nd order>
</object>
....
</array>
这意味着我需要向每个 bfa:object.
插入一个位置元素
我试过这种方法,但没有成功。
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bfa:object">
<xsl:copy-of select="."/>
<Location>USA</Location>
</xsl:template>
你可以这样做:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sap="urn:com.sap.b1i.bizprocessor:bizatoms"
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="sap:object">
<xsl:copy>
<xsl:apply-templates/>
<xsl:element name="Locations" namespace="urn:com.sap.b1i.bizprocessor:bizatoms">USA</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我是 XSLT 的新手。 我正在尝试向从 API 接收到的多个对象添加元素。 所以这是从 API 收到的示例 XML:
<array xmlns="urn:com.sap.b1i.bizprocessor:bizatoms" name="orders">
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<bunch of info here 1st order>
</object>
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<bunch of info here 2nd order>
</object>
....
</array>
输入是这样的,我需要这样的输出:
<array xmlns="urn:com.sap.b1i.bizprocessor:bizatoms" name="orders">
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<Location>USA</Location>
<bunch of info here 1st order>
</object>
<object xmlns="urn:com.sap.b1i.bizprocessor:bizatoms">
<Location>USA</Location>
<bunch of info here 2nd order>
</object>
....
</array>
这意味着我需要向每个 bfa:object.
插入一个位置元素我试过这种方法,但没有成功。
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bfa:object">
<xsl:copy-of select="."/>
<Location>USA</Location>
</xsl:template>
你可以这样做:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sap="urn:com.sap.b1i.bizprocessor:bizatoms"
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="sap:object">
<xsl:copy>
<xsl:apply-templates/>
<xsl:element name="Locations" namespace="urn:com.sap.b1i.bizprocessor:bizatoms">USA</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>