xml – 按照XSL参数的名称删除元素和/或属性

前端之家收集整理的这篇文章主要介绍了xml – 按照XSL参数的名称删除元素和/或属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是根据 XML文件中的名称(“removeMe”)删除不需要的元素和属性的工作:
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2.  
  3. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4.  
  5. <xsl:template match="node() | @*" name="identity">
  6. <xsl:copy>
  7. <xsl:apply-templates select="node() | @*"/>
  8. </xsl:copy>
  9. </xsl:template>
  10.  
  11. <xsl:template match="removeMe"/>
  12. </xsl:stylesheet>

问题是它不区分元素和属性,名称是硬编码的,它只能取一个名字.如何重写这些可以使用以下几个输入参数来删除一个或多个特定元素和/或属性

  1. <xsl:param name="removeElementsNamed"/>
  2. <xsl:param name="removeAttributesNamed"/>

期望的结果是能够移除一个或多个元素和/或一个或多个属性,同时仍然区分元素和属性(换句话说,应该可以移除所有“时间”元素,而不必除去所有的“时间”属性).

虽然我在这一轮需要XSLT 1.0,XSLT 2.0解决方案在接受和其他答案可能对他人有用.

这种转变:
  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4.  
  5. <xsl:param name="removeElementsNamed" select="'x'"/>
  6.  
  7. <xsl:template match="node()|@*" name="identity">
  8. <xsl:copy>
  9. <xsl:apply-templates select="node()|@*"/>
  10. </xsl:copy>
  11. </xsl:template>
  12.  
  13. <xsl:template match="*">
  14. <xsl:if test="not(name() = $removeElementsNamed)">
  15. <xsl:call-template name="identity"/>
  16. </xsl:if>
  17. </xsl:template>
  18. </xsl:stylesheet>

当应用于任何XML文档时,请说:

  1. <t>
  2. <a>
  3. <b/>
  4. <x/>
  5. </a>
  6. <c/>
  7. <x/>
  8. <d/>
  9. </t>

产生所需的正确结果 – 源XML文档的副本,其中具有名称为$removeElementsNamed参数的值的元素的任何出现被删除

  1. <t>
  2. <a>
  3. <b/>
  4. </a>
  5. <c/>
  6. <d/>
  7. </t>

请注意:In XSLT 1.0 it is syntactically illegal to have a variable or parameter reference inside a template match pattern.这就是为什么@JanThomä和@treeMonkey的解决方案都会引发任何与XSLT 1.0兼容的处理器的错误.

更新:这是一个更复杂的解决方案,允许将元素名称的管道分隔列表删除,以传递给转换:

  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4. <xsl:strip-space elements="*"/>
  5.  
  6. <xsl:param name="removeElementsNamed" select="'|x|c|'"/>
  7.  
  8. <xsl:template match="node()|@*" name="identity">
  9. <xsl:copy>
  10. <xsl:apply-templates select="node()|@*"/>
  11. </xsl:copy>
  12. </xsl:template>
  13.  
  14. <xsl:template match="*">
  15. <xsl:if test=
  16. "not(contains($removeElementsNamed,concat('|',name(),'|' )
  17. )
  18. )
  19. ">
  20. <xsl:call-template name="identity"/>
  21. </xsl:if>
  22. </xsl:template>
  23. </xsl:stylesheet>

当应用于同一XML文档(上图)时,转换再次产生所需的正确输出 – 源XML文档中所有元素的名称在$removeElementsNamed参数中指定 – 已删除

  1. <t>
  2. <a>
  3. <b/>
  4. </a>
  5. <d/>
  6. </t>

Update2:与Update1相同的转换,但是用XSLT 2.0编写:

  1. <xsl:stylesheet version="2.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4. <xsl:strip-space elements="*"/>
  5.  
  6. <xsl:param name="removeElementsNamed" select="'|x|c|'"/>
  7.  
  8. <xsl:template match="node()|@*" name="identity">
  9. <xsl:copy>
  10. <xsl:apply-templates select="node()|@*"/>
  11. </xsl:copy>
  12. </xsl:template>
  13.  
  14. <xsl:template match=
  15. "*[name() = tokenize($removeElementsNamed,'\|')]"/>
  16. </xsl:stylesheet>

更新:OP已经添加了要求,也可以删除所有具有特定名称属性.

这是一个略微修改的转换,以适应这个新的要求:

  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4. <xsl:strip-space elements="*"/>
  5.  
  6. <xsl:param name="removeElementsNamed" select="'x'"/>
  7. <xsl:param name="removeAttributesNamed" select="'n'"/>
  8.  
  9. <xsl:template match="node()|@*" name="identity">
  10. <xsl:copy>
  11. <xsl:apply-templates select="node()|@*"/>
  12. </xsl:copy>
  13. </xsl:template>
  14.  
  15. <xsl:template match="*">
  16. <xsl:if test="not(name() = $removeElementsNamed)">
  17. <xsl:call-template name="identity"/>
  18. </xsl:if>
  19. </xsl:template>
  20.  
  21. <xsl:template match="@*">
  22. <xsl:if test="not(name() = $removeAttributesNamed)">
  23. <xsl:call-template name="identity"/>
  24. </xsl:if>
  25. </xsl:template>
  26. </xsl:stylesheet>

当这个转换被应用在下面的XML文档(以前使用但添加了几个属性的时候):

  1. <t>
  2. <a>
  3. <b m="1" n="2"/>
  4. <x/>
  5. </a>
  6. <c/>
  7. <x/>
  8. <d n="3"/>
  9. </t>

产生所需的正确结果(所有名为x的元素和名为n的所有属性都被删除):

  1. <t>
  2. <a>
  3. <b m="1"/>
  4. </a>
  5. <c/>
  6. <d/>
  7. </t>

UPDATE2:再次要求OP,我们现在实现了通过管道分隔的名称列表的功能,以删除具有这些名称的元素,并分别使用以管道分隔的名称列表,以删除具有以下名称属性

  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4. <xsl:strip-space elements="*"/>
  5.  
  6. <xsl:param name="removeElementsNamed" select="'|c|x|'"/>
  7. <xsl:param name="removeAttributesNamed" select="'|n|p|'"/>
  8.  
  9. <xsl:template match="node()|@*" name="identity">
  10. <xsl:copy>
  11. <xsl:apply-templates select="node()|@*"/>
  12. </xsl:copy>
  13. </xsl:template>
  14.  
  15. <xsl:template match="*">
  16. <xsl:if test=
  17. "not(contains($removeElementsNamed,'|')
  18. )
  19. )
  20. ">
  21. <xsl:call-template name="identity"/>
  22. </xsl:if>
  23. </xsl:template>
  24.  
  25. <xsl:template match="@*">
  26. <xsl:if test=
  27. "not(contains($removeAttributesNamed,'|')
  28. )
  29. )
  30. ">
  31. <xsl:call-template name="identity"/>
  32. </xsl:if>
  33. </xsl:template>
  34. </xsl:stylesheet>

当将此转换应用于以下XML文档时:

  1. <t>
  2. <a p="0">
  3. <b m="1" n="2"/>
  4. <x/>
  5. </a>
  6. <c/>
  7. <x/>
  8. <d n="3"/>
  9. </t>

产生所需的正确结果(名称为c和x的元素以及名称为n和p的属性删除):

  1. <t>
  2. <a>
  3. <b m="1"/>
  4. </a>
  5. <d/>
  6. </t>

猜你在找的XML相关文章