【问题标题】:Flatten XML file with XML changing the labels according to their nesting position使用 XML 展平 XML 文件,根据它们的嵌套位置更改标签
【发布时间】:2013-09-25 15:38:20
【问题描述】:

我是 XSL 的新手,我正在尝试将具有以下结构的 XML 文件展平,目的是在 InDesign 中使用它(真正的 XML 结构要复杂得多,实际上遵循 NLM 架构,但下面的示例应该努力说明我需要什么):

例如

 <section>
    <p> just normal text here 1</p>
    <section>
        <p>just normal text for this section</p>
    </section>
    <p>just normal text here 2</p>
    <section>
        <p>just more text</p>
        <section>
            <p>this is the text in the deepest section </p>
        </section>
        <p>and even more text </p>
    </section>
        <p>just normal text here 3</p>
  </section>

使用以下 XSLT,我几乎已经完成了我需要的工作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
        <xsl:apply-templates />
</xsl:template>


<xsl:template match="section">
   <xsl:variable name="sectionlevel">
      <xsl:value-of select="count(ancestor::section)" />
   </xsl:variable>
   <xsl:element name="s{$sectionlevel}">  

       <xsl:apply-templates select="descendant::section" />
       <xsl:copy-of select="*[local-name() != 'section']"/>

   </xsl:element>
</xsl:template>             
</xsl:stylesheet>

我使用这段代码得到的输出如下所示,这很好,但问题是我需要保持元素的顺序。我只需要更改部分元素名称并保持其他所有内容相同:

<?xml version="1.0" encoding="utf-8"?>
<s0>
  <s1>
    <p> just normal text for this section</p>
  </s1>
  <s1>
    <s2>
      <p>this is the text in the deepest section </p>
    </s2>
    <p>just more text</p>
    <p>and even more text </p>
  </s1>
  <s2>
    <p>this is the text in the deepest section </p>
  </s2>
  <p> just normal text here 1</p>
  <p>just normal text here 2</p>
  <p>just normal text here 3</p>
</s0>

如你所见

此示例中的

元素被移动到 section 元素内的末尾。我应该如何编码 XSL 转换,以便保留所有原始 XML 顺序和结构并只更改部分标签?

【问题讨论】:

    标签: xslt adobe-indesign flatten


    【解决方案1】:

    这是你所期望的吗?

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
        <!-- Identity to copy all elements -->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="section" >
            <xsl:element name="s{count(ancestor::section)}">
                <xsl:apply-templates select="@*|node()" />
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    这将输出:

    <?xml version="1.0" encoding="UTF-8"?>
    <s0>
        <p> just normal text here 1</p>
        <s1>
            <p>just normal text for this section</p>
        </s1>
        <p>just normal text here 2</p>
        <s1>
            <p>just more text</p>
            <s2>
                <p>this is the text in the deepest section </p>
            </s2>
            <p>and even more text </p>
        </s1>
        <p>just normal text here 3</p>
    </s0>
    

    【讨论】:

    • 是的,这正是我所期望的。就在您发布答案之前,我将发布一个类似但使用以下内容复制所有元素的内容:
    • 您能否评估一下这是否会在我的方法的任何特定情况下输出不同的结果??
    • 您的评论不完整或有问题。模板匹配之间没有任何要匹配的内容。我更喜欢使用身份模板。少写
    猜你喜欢
    • 1970-01-01
    • 2022-06-11
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多