简体   繁体   English

为什么我的XSLT被忽略? XML输出没有变化

[英]Why is my XSLT being ignored? The XML Output has no change

I wish to translate an XML file so that the subheaders 'Subheader1' and 'Subheader2' are removed so that a program I have can read the XML data and identify the information under 'Lines/Line'. 我希望翻译一个XML文件,以便删除子标题“ Subheader1”和“ Subheader2”,以便我所拥有的程序可以读取XML数据并标识“行/行”下的信息。

In this case the best way to achieve this would be to remove the subheaders and keep all the elements under a single header. 在这种情况下,实现此目的的最佳方法是删除子标题并将所有元素保留在单个标题下。

I have made attempts to achieve the problem that I am having. 我已经尝试解决我遇到的问题。 But, the XSLT that I have made is just being ignored. 但是,我所做的XSLT只是被忽略了。 So far I have achieved just exporting the element data. 到目前为止,我已经实现了仅导出元素数据。 But the element headers are ignored. 但是元素标头被忽略。 Or, in this case, the XSLT just exports the exact same format. 或者,在这种情况下,XSLT仅导出完全相同的格式。 I just don't understand why this is happening. 我只是不明白为什么会这样。

XML Input XML输入

<Header xmlns="urn:stuff:xml:ns:neo">
   <ID>1</ID>
   <Department>350</Department>
   <Date>2019-03-07T14:38:00</Date>
   <Order_Number>12345</Order_Number>
   <Comment/>
   <Status>Picking</Status>
   <Subheader1>
      <Subheader2>
         <Product>12510101</Product>
         <Quantity>1</Quantity>
         <Comment_Line/>
         <Location>R11</Location>
      </Subheader2>
   </Subheader1>
</Header>

XSLT XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:stuff:xml:ns:neo">
<xsl:output method="xml" 
omit-xml-declaration="yes" 
indent="yes"/>

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

  <xsl:template match="Subheader1" />
  <xsl:template match="Subheader2" />

</xsl:stylesheet>

XML Output XML输出

<Header xmlns="urn:stuff:xml:ns:neo">
   <ID>1</ID>
   <Department>350</Department>
   <Date>2019-03-07T14:38:00</Date>
   <Order_Number>12345</Order_Number>
   <Comment/>
   <Status>Picking</Status>
   <Product>12510101</Product>
   <Quantity>1</Quantity>
   <Comment_Line/>
   <Location>R11</Location>
</Header>

My Current Output 我目前的输出


1
350
360
361
2019-03-07T14:38:00
615619

Active


1
12510101
50153061
23663
Active

RM STD

I'm expecting the XML output that I have added above. 我期望上面添加的XML输出。 Any help with this including what needs to be done and why would be appreciated. 任何与此有关的帮助,包括需要做的事情和原因,将不胜感激。 Been trying to fix this for some time and just at a point where I cannot understand why this is occurring. 试图修复此问题已经有一段时间了,只是在我无法理解为什么会发生这一点上。

General reasons XSLT might be "ignored:" XSLT可能被“忽略”的一般原因:

  1. The input XML is in one or more namespaces, explicitly or by default, and your XSLT is matching against elements in no namespaces. 输入XML在一个或多个名称空间中(显式地或默认情况下),并且您的XSLT与没有名称空间中的元素匹配。 See the XSLT below for how to match against namespaced elements. 有关如何与命名空间元素匹配的信息,请参见下面的XSLT。

  2. There are typos or case disagreement between element names in the input XML and the XSLT. 输入XML和XSLT中的元素名称之间存在拼写错误或大小写不一致。

  3. None of the provided XSLT templates match, and only the built-in template rules remain to perform the transformation. 提供的XSLT模板均不匹配,仅保留内置模板规则来执行转换。

Side note: Your current output isn't really what the posted XSLT would generated for the posted XML input file. 旁注:您当前的输出实际上不是为已发布的XML输入文件生成的已发布XSLT。 It would copy the input XSLT to the output XSLT per the identity transform, and your two Line and Lines templates would have no effect because there are no Line or Lines elements in your input XML. 它将按照身份转换将输入XSLT复制到输出XSLT,并且您的两个LineLines模板将无效,因为输入XML中没有LineLines元素。

Your specific case: You can unwrap the contents of the Subheader1 and Subheader2 elements via the following XSLT: 您的具体情况:您可以通过以下XSLT打开Subheader1Subheader2元素的内容:

XSLT XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:neo="urn:stuff:xml:ns:neo">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

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

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

</xsl:stylesheet>

XML Output XML输出

<Header xmlns="urn:stuff:xml:ns:neo">
  <ID>1</ID>
  <Department>350</Department>
  <Date>2019-03-07T14:38:00</Date>
  <Order_Number>12345</Order_Number>
  <Comment/>
  <Status>Picking</Status>


      <Product>12510101</Product>
      <Quantity>1</Quantity>
      <Comment_Line/>
      <Location>R11</Location>


</Header>

Text nodes 文字节点

You can eliminate the extra whitespace via 您可以通过以下方式消除多余的空格

  <xsl:strip-space elements="neo:Subheader1 neo:Subheader2"/>

added to xsl:stylesheet , or you might want to only copy the element ( * ) children of those two elements rather than all children node() s: 添加到xsl:stylesheet ,或者您可能只想复制这两个元素的子元素* ),而不是所有子元素node()

  <xsl:template match="neo:Subheader1 | neo:Subheader2">
    <xsl:apply-templates select="* | @*"/>
  </xsl:template>

Note that | @* 需要注意的是| @* | @* isn't really needed here but could help if your actual case has attributes. | @*在这里并不是真正需要的,但是如果您的实际案例具有属性,则可以提供帮助。 Similarly, note that you can add back processing-instruction()|comment() if such children types are desired after changing node() to * . 同样,请注意,如果在将node()更改为*之后需要此类子类型,则可以添加回processing-instruction()|comment()

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

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