简体   繁体   English

XSLT脚本摆脱嵌套的子节点

[英]XSLT script to get rid of nested child nodes

Input XML: 输入XML:

<tree>
  <node name="GrandParent">
    <node name="Parent1">
      <qualifier>Fat</qualifier>
    </node>
    <node name="Parent2">
      <qualifier>Slim</qualifier>
      <node name="Child1">
        <qualifier>Tall</qualifier>
      </node>
      <node name="Child2">
        <qualifier>Short</qualifier>
      </node>
    </node>
  </node>
</tree>

Required output: 要求的输出:

<tree>
  <node name="GrandParent-Parent1">
    <qualifier>Fat</qualifier>
  </node>
  <node name="GrandParent-Parent2">
    <qualifier>Slim</qualifier>
  </node>
  <node name="GrandParent-Parent2-Child1">
    <qualifier>Tall</qualifier>
  </node>
  <node name="GrandParent-Parent2-Child2">
    <qualifier>Short</qualifier>
  </node>
</tree>

I want to get an optimized XSLT script for achieving the same. 我想获得一个优化的XSLT脚本来实现相同目的。 Please help me. 请帮我。

(Note: The input given is just a sample. The child nodes can be nested to any depth. ) (注意:给定的输入只是一个示例。子节点可以嵌套到任何深度。)

Here are some hints to point you in the right direction: 以下是一些提示,可指示您正确的方向:

  • Get to know the <xsl:template> and <xsl:apply-templates> instructions; 了解<xsl:template><xsl:apply-templates>指令; they will be the backbone of a solution for this problem. 它们将是解决此问题的骨干。
  • Get to know the `concat()' function, as well. 也了解`concat()'函数。 This will assist with the naming scheme change you are looking to make. 这将有助于您要进行的命名方案更改。
  • Become comfortable with XPath axes (in particular, parent::* , ancestor::* , and relative XPaths like ../ or ../../ . 熟悉XPath轴(尤其是parent::*ancestor::*和相对的XPath,例如../../../

Additionally, take a look at this question (and subsequent answers) - it might be useful in your scenario, as well: Merge successive descendant nodes into one 此外,请查看此问题(及后续答案)-在您的方案中也可能有用:将连续的后代节点合并为一个

It sounds like you have a hard deadline tomorrow, but going forward, I would recommend taking the time to read good materials on XSLT/XPath. 听起来您明天的工作期限很艰巨,但是展望未来,我建议您花些时间阅读XSLT / XPath上的优秀材料。 You can find several here: https://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589 您可以在这里找到几个: https : //stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM