XSLT:将平面 XML 转换为分层 XML

asx

我是 xml 新手,我很难理解 XSLT 是如何工作的。你能帮我修复我的 xslt 文件中的一些错误吗?我想转换这个输入文件:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <parent><string >AAA</string></parent>
    <nb><string >2</string></nb>
    <child1>aaa-1</child1>
    <child1>aaa-2</child1>
    <parent><string >BBB</string></parent>
    <nb><string>1</string></nb>
    <child2>bbb-1</child2>
    <parent><string >CCC</string></parent>
    <nb><string >0</string></nb>
</data>

进入 :

<?xml version="1.0" encoding="utf-8"?>
<data>
    <parent>
        <string >AAA</string>
        <nb><string >2</string></nb>
        <child1>aaa-1</child1>
        <child1>aaa-2</child1></parent>
    <parent>
        <string >BBB</string>
        <nb><string >1</string></nb>
        <child2>bbb-1</child2></parent>
    <parent>
        <string >CCC</string>
        <nb><string >0</string></nb></parent>
</data>

规则是:

  1. “nb”节点表示每个父节点的子节点数。它可以是 0。

  2. 节点“child1”和“child2”是不同的。它们很复杂,有嵌套循环等。我上面的输入文件是为了演示而简化的。
    我想,我必须使用“复制”指令。

  3. 对我来说困难的是:

    • 对于每个父节点,我必须读取当前父节点之后定义的子节点数(“nb”)
    • 当“父”值为“AAA”
      时,当“父”值为 !=AAAA 时,我必须读取“child1”,然后我必须读取“childe2”节点。

这是我的 XSLT 文件,结果与预期不完全一样:

    <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="/">
        <xsl:comment>--- </xsl:comment>
        <xsl:comment>1 : parent nodes </xsl:comment>
        <xsl:for-each select ="/data/parent">
            <p> 
                <string>
                    <xsl:value-of select="string"/>
                </string>
                <xsl:comment>======================= </xsl:comment>
                <xsl:comment>2 : nb nodes (how many childs for a parent ) </xsl:comment>
                <xsl:for-each select ="/data/nb">
                    <xsl:if test="((position() &lt; 2) and (normalize-space(position() &gt;= 1)))">
                        <xsl:comment>Ex. for tThe first value only </xsl:comment>
                        <xsl:comment>How to do a dynamic test here (expected : AAA->3 (first nb value), BBB->1 (second nb value) ...)  ?</xsl:comment>
                        <xsl:comment>How to synchronise loop on parent and nb  ?</xsl:comment>
                        <nb>
                            <string>
                                <xsl:value-of select="string"/>
                            </string>
                        </nb>
                    </xsl:if>
                </xsl:for-each>
                <xsl:comment>======================= </xsl:comment>
                <xsl:comment>3 : child nodes </xsl:comment>
                <xsl:comment>How to manage the position and number of nodes to read ?</xsl:comment>
                <xsl:comment>Test 'string =AAA' is KO : always child2 </xsl:comment>
                <xsl:choose>
                    <xsl:when test='string =AAA'>
                        <xsl:copy-of select="/*/child1" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="/*/child2" />
                    </xsl:otherwise>
                </xsl:choose>               
            </p>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

提前致谢
最好的问候

马丁·霍南

如果数据是一致的,也就是说,如果元素指示child1/child2元素数量nbparent总是存在的,那么你可以简单地处理不同类型的parent元素

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:output indent="yes"/>

  <xsl:template match="data">
      <xsl:copy>
          <xsl:apply-templates select="parent"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="parent[string = 'AAA']">
      <xsl:copy>
          <xsl:variable name="n" select="following-sibling::nb[1]"/>
          <xsl:copy-of select="string, following-sibling::nb[1], following-sibling::child1[position() &lt;= $n]"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="parent[string != 'AAA']">
      <xsl:copy>
          <xsl:variable name="n" select="following-sibling::nb[1]"/>
          <xsl:copy-of select="string, following-sibling::nb[1], following-sibling::child2[position() &lt;= $n]"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/94hvTzk/0有那个样本,你也可以在 XSLT 1 中这样做

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output indent="yes"/>

  <xsl:template match="data">
      <xsl:copy>
          <xsl:apply-templates select="parent"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="parent[string = 'AAA']">
      <xsl:copy>
          <xsl:variable name="n" select="following-sibling::nb[1]"/>
          <xsl:copy-of select="string | following-sibling::nb[1] | following-sibling::child1[position() &lt;= $n]"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="parent[string != 'AAA']">
      <xsl:copy>
          <xsl:variable name="n" select="following-sibling::nb[1]"/>
          <xsl:copy-of select="string | following-sibling::nb[1] | following-sibling::child2[position() &lt;= $n]"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

在线https://xsltfiddle.liberty-development.net/94hvTzk/1

如果数据不一致,那么至少在 XSLT 2 或 3for-each-group select="*" group-starting-with="parent"中,在data元素的上下文中识别属于一起的元素并parent在结果中创建包装器将是一项轻松的工作

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章