Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSL转换新手需要帮助扁平化XML文件_Xml_Xslt - Fatal编程技术网

XSL转换新手需要帮助扁平化XML文件

XSL转换新手需要帮助扁平化XML文件,xml,xslt,Xml,Xslt,我正在尝试转换此文件: <?xml version="1.0" encoding="utf-8"?> <x12errors> <header> <errors> <issue> <message>Wrong value</message> <path>path to bad value</path>

我正在尝试转换此文件:

<?xml version="1.0" encoding="utf-8"?> 
<x12errors>  
  <header>  
    <errors>  
      <issue>  
        <message>Wrong value</message>  
        <path>path to bad value</path>  
      </issue>  
      <issue>  
        <message>Missing value</message>  
        <path>path where missing value should be</path>  
      </issue>  
    </errors>  
    <warnings>  
      <issue>  
        <message>Value too small</message>  
        <path>path to value</path>  
      </issue>  
    </warnings>  
  </header>  
  <boxes>  
    <box>  
      <boxName>cardboard</boxName>  
      <boxStyleNum>12345</boxStyleNum>  
      <errors>  
        <issue>  
          <message>Box too small</message>  
          <path>path to box size</path>  
        </issue>  
      </errors>  
      <warnings>  
        <issue>  
          <message>Box flaps off center</message>  
          <path>path to box measurements</path>  
        </issue>  
      </warnings>  
      <wrappings>  
        <wrapping>  
          <material>bubble wrap</material>
          <dimensions>9x12</dimensions>
          <errors>
            <issue>
              <message>Wrong material</message>
              <path>path</path>
            </issue>
          </errors>
          <warnings>
            <issue>
              <message>Prefer different color</message>
              <path>path to value</path>
            </issue>
          </warnings>
        </wrapping>
      </wrappings>
    </box>
  </boxes>
</x12errors>
<?xml version="1.0" encoding="utf-8"?>
<x12errors>
  <header>
    <headerMsg><type>E</type><msgText>Wrong value</msgText></headerMsg>
    <headerMsg><type>E</type><msgText>Missing value</msgText></headerMsg>
    <headerMsg><type>W</type><msgText>Value too small</msgText></headerMsg>
  </header>
  <boxes>
    <box>
      <boxName>cardboard</boxName>
      <boxStyleNum>12345</boxStyleNum>
      <boxMsg><type>E</type><msgText>Box too small</msgText></boxMsg>
      <boxMsg><type>W</type><msgText>Box flaps off center</msgText></boxMsg>
      <wrappings>
        <wrapping>
          <material>bubble wrap</material>
          <dimensions>9x12</dimensions>
          <wrappingMsg><type>E</type><msgText>Wrong material</msgText></wrappingMsg>
          <wrappingMsg><type>E</type><msgText>Prefer different color</msgText></wrappingMsg>
        </wrapping>
      </wrappings>
    </box>
  </boxes>
</x12errors>

错误值
坏值路径
缺失值
缺少值的路径
价值太小
价值之路
硬纸板
12345
盒子太小了
框大小的路径
箱盖偏离中心
盒子测量路径
泡泡纸
9x12
错误的材料
路径
喜欢不同的颜色
价值之路
要删除此文件,请执行以下操作:

<?xml version="1.0" encoding="utf-8"?> 
<x12errors>  
  <header>  
    <errors>  
      <issue>  
        <message>Wrong value</message>  
        <path>path to bad value</path>  
      </issue>  
      <issue>  
        <message>Missing value</message>  
        <path>path where missing value should be</path>  
      </issue>  
    </errors>  
    <warnings>  
      <issue>  
        <message>Value too small</message>  
        <path>path to value</path>  
      </issue>  
    </warnings>  
  </header>  
  <boxes>  
    <box>  
      <boxName>cardboard</boxName>  
      <boxStyleNum>12345</boxStyleNum>  
      <errors>  
        <issue>  
          <message>Box too small</message>  
          <path>path to box size</path>  
        </issue>  
      </errors>  
      <warnings>  
        <issue>  
          <message>Box flaps off center</message>  
          <path>path to box measurements</path>  
        </issue>  
      </warnings>  
      <wrappings>  
        <wrapping>  
          <material>bubble wrap</material>
          <dimensions>9x12</dimensions>
          <errors>
            <issue>
              <message>Wrong material</message>
              <path>path</path>
            </issue>
          </errors>
          <warnings>
            <issue>
              <message>Prefer different color</message>
              <path>path to value</path>
            </issue>
          </warnings>
        </wrapping>
      </wrappings>
    </box>
  </boxes>
</x12errors>
<?xml version="1.0" encoding="utf-8"?>
<x12errors>
  <header>
    <headerMsg><type>E</type><msgText>Wrong value</msgText></headerMsg>
    <headerMsg><type>E</type><msgText>Missing value</msgText></headerMsg>
    <headerMsg><type>W</type><msgText>Value too small</msgText></headerMsg>
  </header>
  <boxes>
    <box>
      <boxName>cardboard</boxName>
      <boxStyleNum>12345</boxStyleNum>
      <boxMsg><type>E</type><msgText>Box too small</msgText></boxMsg>
      <boxMsg><type>W</type><msgText>Box flaps off center</msgText></boxMsg>
      <wrappings>
        <wrapping>
          <material>bubble wrap</material>
          <dimensions>9x12</dimensions>
          <wrappingMsg><type>E</type><msgText>Wrong material</msgText></wrappingMsg>
          <wrappingMsg><type>E</type><msgText>Prefer different color</msgText></wrappingMsg>
        </wrapping>
      </wrappings>
    </box>
  </boxes>
</x12errors>

错误值
发射值
W值太小
硬纸板
12345
电子邮箱太小了
WBox襟翼偏离中心
泡泡纸
9x12
错误的材料
不同颜色
我有下面的xsl文件已经关闭,但是它在那里留下了“errors”和“warnings”标记,我不知道为什么!有人能帮我吗

以下是我当前的xsl:

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

  <xsl:output method="xml" indent="no"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="header//errors/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="header//errors/issue/message">
    <headerMsg>
      <type>E</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </headerMsg>
  </xsl:template>

  <xsl:template match="header/warnings/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="header//warnings/issue/message">
    <headerMsg>
      <type>W</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </headerMsg>
  </xsl:template>

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

  <xsl:template match="box/errors/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="box//errors/issue/message">
    <boxMsg>
      <xsl:apply-templates select="../../boxName"/>
      <xsl:apply-templates select="../../boxStyle"/>
      <type>E</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </boxMsg>
  </xsl:template>

  <xsl:template match="box/warnings/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="box//warnings/issue/message">
    <boxMsg>
      <xsl:apply-templates select="../../boxName"/>
      <xsl:apply-templates select="../../boxStyle"/>
      <type>W</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </boxMsg>
  </xsl:template>

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

  <xsl:template match="wrapping/errors/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="wrapping//errors/issue/message">
    <wrappingMsg>
      <xsl:apply-templates select="../../material"/>
      <xsl:apply-templates select="../../dimensions"/>
      <type>E</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </wrappingMsg>
  </xsl:template>

  <xsl:template match="wrapping/warnings/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="wrapping//warnings/issue/message">
    <wrappingMsg>
      <xsl:apply-templates select="../../material"/>
      <xsl:apply-templates select="../../dimensions"/>
      <type>W</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </wrappingMsg>
  </xsl:template>


</xsl:stylesheet>

E
W
E
W
E
W

谢谢!劳里

我认为您在一开始就“要求”所有节点。你会得到很多原创的东西。只需在XSLT中删除它:

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

我可以得到一个输出:

<?xml version="1.0" encoding="utf-8"?>  
<headerMsg><type>E</type><msgText>Wrong value</msgText></headerMsg>  
<headerMsg><type>E</type><msgText>Missing value</msgText></headerMsg>  
<headerMsg><type>W</type><msgText>Value too small</msgText></headerMsg>  
        cardboard  
        12345  
        <boxMsg><type>E</type><msgText>Box too small</msgText></boxMsg>  
        <boxMsg><type>W</type><msgText>Box flaps off center</msgText></boxMsg>  
        bubble wrap
        9x12
        <wrappingMsg><type>E</type><msgText>Wrong material</msgText></wrappingMsg>
        <wrappingMsg><type>W</type><msgText>Prefer different color</msgText></wrappingMsg>

错误值
发射值
W值太小
硬纸板
12345
电子邮箱太小了
WBox襟翼偏离中心
泡泡纸
9x12
错误的材料
我指的是不同的颜色
此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="message" exclude-result-prefixes="m">
    <m:header>
        <m:message type="E">Wrong value</m:message>
        <m:message type="E">Missing value</m:message>
        <m:message type="W">Value too small</m:message>
    </m:header>
    <m:box>
        <m:message type="E">Box too small</m:message>
        <m:message type="W">Box flaps off center</m:message>
    </m:box>
    <m:wrapping>
        <m:message type="E">Wrong material</m:message>
        <m:message type="E">Prefer different color</m:message>
    </m:wrapping>
    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="path"/>
    <xsl:template match="errors|warnings|issue">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="message">
        <xsl:element name="{local-name(../../..)}Msg">
            <type>
                <xsl:value-of select="document('')/*/m:*[local-name()=local-name(current()/../../..)]/*[.=current()]/@type"/>
            </type>
            <msgText>
                <xsl:value-of select="."/>
            </msgText>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

错误值
缺失值
价值太小
盒子太小了
箱盖偏离中心
错误的材料
喜欢不同的颜色
输出:

<x12errors>
    <header>
        <headerMsg>
            <type>E</type>
            <msgText>Wrong value</msgText>
        </headerMsg>
        <headerMsg>
            <type>E</type>
            <msgText>Missing value</msgText>
        </headerMsg>
        <headerMsg>
            <type>W</type>
            <msgText>Value too small</msgText>
        </headerMsg>
    </header>
    <boxes>
        <box>
            <boxName>cardboard</boxName>
            <boxStyleNum>12345</boxStyleNum>
            <boxMsg>
                <type>E</type>
                <msgText>Box too small</msgText>
            </boxMsg>
            <boxMsg>
                <type>W</type>
                <msgText>Box flaps off center</msgText>
            </boxMsg>
            <wrappings>
                <wrapping>
                    <material>bubble wrap</material>
                    <dimensions>9x12</dimensions>
                    <wrappingMsg>
                        <type>E</type>
                        <msgText>Wrong material</msgText>
                    </wrappingMsg>
                    <wrappingMsg>
                        <type>E</type>
                        <msgText>Prefer different color</msgText>
                    </wrappingMsg>
                </wrapping>
            </wrappings>
        </box>
    </boxes>
</x12errors>

E
错误值
E
缺失值
W
价值太小
硬纸板
12345
E
盒子太小了
W
箱盖偏离中心
泡泡纸
9x12
E
错误的材料
E
喜欢不同的颜色
编辑1:更好的解释。 注意:“身份转换”(模板[@name='idenity'])只需按原样复制输入源即可。一些元素被忽略(不是复制而是将模板应用于孩子):
错误
警告
问题
<代码>路径元素用空模板条带化。内联映射:
document(“”)
evaluate到样式表文档根;处理程序会忽略XSLT命名空间以外的其他命名空间中的顶级元素,但我们可以选择它们(在这种情况下,那些本地名等于
message
祖父本地名的孩子,然后是那些字符串值等于
message
字符串值的孩子,最后是他们的
type
atribute)

小建议:)。如果有任何XML具有自己的XSL,您可以使用XEP检查它们是否相互对应,否则它将显示您的错误。。。 有关XEP的更多信息,请访问论坛

我也试过了,但我不知道如何只要求我想要的节点。我对xsl知之甚少,到目前为止,我几乎完全是通过反复试验才弄明白了自己的想法。哇!这是一个巧妙的解决方案。你能通过走读来启发一下吗?我会非常高兴的。你已经有我的+1了。