如何扩展现有XSLT实现XML属性忽略与<Name>节点属性新增
Here's the extended XSLT that meets all your requirements, building on your existing code:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <!-- Identity template: copies all nodes/attributes by default --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <!-- 1. Ignore Classification's ParentID attribute --> <xsl:template match="Classification/@ParentID"/> <!-- 2. Ignore STEP-ProductInformation's WorkspaceID attribute --> <xsl:template match="STEP-ProductInformation/@WorkspaceID"/> <!-- Keep existing rule: suppress Name nodes with ContextID='de_DE' --> <xsl:template match="Name[@ContextID='de_DE']"/> <!-- 3. Add Translation_Status="UpToDate" to non-suppressed Name nodes --> <xsl:template match="Name[not(@ContextID='de_DE')]"> <xsl:copy> <xsl:attribute name="Translation_Status">UpToDate</xsl:attribute> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <!-- Keep existing rule: suppress Value nodes with ContextID='de_DE' --> <xsl:template match="Value[@ContextID='de_DE']"/> </xsl:stylesheet>
Key changes explained:
- Ignoring specific attributes: We added two targeted templates that match the exact attributes you want to exclude. Since these templates have no content, those attributes won't be copied to the output.
- Adding the Translation_Status attribute: We created a template for
Namenodes that aren't suppressed (i.e., those withoutContextID='de_DE'). This template copies the original node, adds the new attribute first, then preserves all existing attributes and child content of the Name node.
When applied to your sample XML, the output will:
- Omit the
ParentIDfrom the Classification node - Omit the
WorkspaceIDfrom the root STEP-ProductInformation node - Add
Translation_Status="UpToDate"to the remaining Name node (the one withContextID='fr_FR') - Keep suppressing the de_DE Name and Value nodes as before
内容的提问来源于stack exchange,提问作者user9020437
相关产品推荐
相关产品推荐

