XSLT 在检查属性是否相同后将 1 xml 中标签的文本值替换为另一个 xml 然后替换文本值
XSLT Replacing text value of the tag in 1 xml with another xml after checking if attribute are the same then replace text value
我是这方面的初学者,我想使用 xslt 来转换我的 xml 文件,但我缺乏这样做的知识和经验。这是我想用 xsl
转换的 file1.xml 的示例
file1.xml
<shop>
<SHOPITEM>
<CATEGORY id="12306">ABCclothes</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="1233">SDFclothes</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="12308">CDFclothes</CATEGORY>
</SHOPITEM>
</shop>
检查 CATEGORY 中的属性 id 是否与 file2.xml 中的 CATEGORY2 中的 id 相同后,
然后将来自 file1.xml 的元素文本 CATEGORY 替换为来自 file2.xml
的 CATEGORY2 元素文本
file2.xml
<ITEM>
<CATEGORY2 id="12308">CDFreplacetext<CATEGORY2>
<CATEGORY2 id="12306">ABCreplacetext<CATEGORY2>
</ITEM>
这是我试图获得的输出
输出:
<shop>
<SHOPITEM>
<CATEGORY id="12306">ABCreplacetext</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="1233">SDFclothes</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="12308">CDFreplacetext</CATEGORY>
</SHOPITEM>
</shop>
如果您可以使用 XSLT 版本 3.0(或 2.0;只需将 xsl:mode
替换为 identity transform),我会使用 xsl:key
...
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="file2" select="document('file2.xml')"/>
<xsl:key name="cat2" match="CATEGORY2" use="@id"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="CATEGORY">
<xsl:copy>
<xsl:apply-templates select="@*,(key('cat2',@id,$file2)/node(),node())[1]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果您受困于 XSLT 1.0,请尝试使用 xsl:choose
...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="file2" select="document('file2.xml')"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CATEGORY">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:choose>
<xsl:when test="$file2//CATEGORY2[@id=current()/@id]">
<xsl:apply-templates select="$file2//CATEGORY2[@id=current()/@id]/node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我是这方面的初学者,我想使用 xslt 来转换我的 xml 文件,但我缺乏这样做的知识和经验。这是我想用 xsl
转换的 file1.xml 的示例file1.xml
<shop>
<SHOPITEM>
<CATEGORY id="12306">ABCclothes</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="1233">SDFclothes</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="12308">CDFclothes</CATEGORY>
</SHOPITEM>
</shop>
检查 CATEGORY 中的属性 id 是否与 file2.xml 中的 CATEGORY2 中的 id 相同后, 然后将来自 file1.xml 的元素文本 CATEGORY 替换为来自 file2.xml
的 CATEGORY2 元素文本file2.xml
<ITEM>
<CATEGORY2 id="12308">CDFreplacetext<CATEGORY2>
<CATEGORY2 id="12306">ABCreplacetext<CATEGORY2>
</ITEM>
这是我试图获得的输出
输出:
<shop>
<SHOPITEM>
<CATEGORY id="12306">ABCreplacetext</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="1233">SDFclothes</CATEGORY>
</SHOPITEM>
<SHOPITEM>
<CATEGORY id="12308">CDFreplacetext</CATEGORY>
</SHOPITEM>
</shop>
如果您可以使用 XSLT 版本 3.0(或 2.0;只需将 xsl:mode
替换为 identity transform),我会使用 xsl:key
...
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="file2" select="document('file2.xml')"/>
<xsl:key name="cat2" match="CATEGORY2" use="@id"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="CATEGORY">
<xsl:copy>
<xsl:apply-templates select="@*,(key('cat2',@id,$file2)/node(),node())[1]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果您受困于 XSLT 1.0,请尝试使用 xsl:choose
...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="file2" select="document('file2.xml')"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CATEGORY">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:choose>
<xsl:when test="$file2//CATEGORY2[@id=current()/@id]">
<xsl:apply-templates select="$file2//CATEGORY2[@id=current()/@id]/node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>