XSLT 转换不提供正确的输出

XSLT transform not providing correct output

我的 XSLT 转换有点问题。

我有以下 XSLT;

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><!-- removes the unrelated elements -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="master_version[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="press_section[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="version[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="task_info_press_section[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="task_info_post_press[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="post_press_version[not(ORDER = //order/ORDERPK)]"/>
   <!-- removes specified nodes from all elements -->
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="ORDER"/>
   <xsl:template match="ORDERPK"/>
   <xsl:template match="PRESS_x0020_SECTION"/>
   <xsl:template match="POST_x0020_PRESS"/>
   <!-- Creates attributes against the ORDER element -->
   <xsl:strip-space elements="*"/>
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="order">
      <order job_id="{job_id}" site_code="{site_code}" replace="{replace}">
         <xsl:apply-templates/>
      </order>
   </xsl:template>
   <xsl:template match="job_id | site_code | replace | master_version"/>
   <!-- identity transform -->
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="order">
      <order job_id="{@job_id}" site_code="{@site_code}" replace="{Replace}">
         <xsl:apply-templates select="node()"/>
         <xsl:copy-of select="../master_version"/>
      </order>
   </xsl:template>
   <xsl:template match="Replace | master_version"/>
   <!-- renames element to specified name -->
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="task_info_press_section">
      <xsl:element name="task_info1">
         <xsl:apply-templates/>
      </xsl:element>
   </xsl:template>
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="task_info_post_press">
      <xsl:element name="task_info2">
         <xsl:apply-templates/>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

这是我改造前的原图XML;

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-01-07T16:58:58">

<order>
    <ORDERPK>3</ORDERPK>
    <job_id>S026500-1</job_id>
    <site_code>DG</site_code>
    <Replace>true</Replace>
    <job_description>TESTING</job_description>
    <order_qty>20000</order_qty>
    <finishing_style>PB</finishing_style>
    <depth>10</depth>
    <width>8</width>
    <cover_pagination>4</cover_pagination>
    <text_pagination>24</text_pagination>
    <delivery_commence_date>19/12/2014</delivery_commence_date>
    <delivery_complete_date>19/12/2014</delivery_complete_date>
    <job_site>DG</job_site>
    <managing_printer>DG</managing_printer>
    <is_managing_printer>True</is_managing_printer>
</order>
<master_version>
    <ORDER>1</ORDER>
    <version_id></version_id>
    <version_code>COMM</version_code>
    <version_common>true</version_common>
    <version_finished>false</version_finished>
    <version_description>Common</version_description>
    <version_nett_qty>176262</version_nett_qty>
    <version_special_qty>10</version_special_qty>
</master_version>
<master_version>
    <ORDER>2</ORDER>
    <version_code>COMM</version_code>
    <version_common>TRUE</version_common>
    <version_finished>FALSE</version_finished>
    <version_description>Common</version_description>
    <version_nett_qty>1900</version_nett_qty>
    <version_special_qty>0</version_special_qty>
</master_version>
<master_version>
    <ORDER>3</ORDER>
    <version_code>COMM</version_code>
    <version_common>true</version_common>
    <version_finished>false</version_finished>
    <version_description>common</version_description>
    <version_nett_qty>20000</version_nett_qty>
    <version_special_qty>0</version_special_qty>
</master_version>
<press_section>
    <ORDER>1</ORDER>
    <signature_id>001</signature_id>
    <sequence_id>1</sequence_id>
    <sequence_alpha>A</sequence_alpha>
    <description>4ppCover</description>
    <pagination>4</pagination>
    <trim_size>10.875 x 8.375</trim_size>
    <folio></folio>
    <data_format></data_format>
    <data_medium></data_medium>
    <data_due></data_due>
</press_section>
<press_section>
    <ORDER>1</ORDER>
    <signature_id>001</signature_id>
    <sequence_id>2</sequence_id>
    <sequence_alpha>A</sequence_alpha>
    <description>240ppText</description>
    <pagination>240</pagination>
    <trim_size>103875 x 8.25</trim_size>
</press_section>
<press_section>
    <ORDER>2</ORDER>
    <signature_id>001</signature_id>
    <sequence_id>1</sequence_id>
    <sequence_alpha>A</sequence_alpha>
    <description>4pp Cover</description>
    <pagination>4</pagination>
    <trim_size>10 x 8</trim_size>
</press_section>
<press_section>
    <ORDER>2</ORDER>
    <signature_id>001</signature_id>
    <sequence_id>2</sequence_id>
    <sequence_alpha>A</sequence_alpha>
    <description>12pp Text</description>
    <pagination>12</pagination>
    <trim_size>10 x 8</trim_size>
</press_section>
<press_section>
    <ORDER>3</ORDER>
    <signature_id>001</signature_id>
    <sequence_id>0</sequence_id>
    <sequence_alpha>A</sequence_alpha>
    <description>4pp Cover</description>
    <pagination>4</pagination>
    <trim_size>10 x 8</trim_size>
</press_section>
<press_section>
    <ORDER>3</ORDER>
    <signature_id>001</signature_id>
    <sequence_id>1</sequence_id>
    <sequence_alpha>A</sequence_alpha>
    <description>24pp Text</description>
    <pagination>24</pagination>
    <trim_size>10 x 8</trim_size>
</press_section>
<version>
    <ORDER>1</ORDER>
    <version_code>COMM</version_code>
</version>
<version>
    <ORDER>2</ORDER>
    <version_code>COMM</version_code>
</version>
<version>
    <ORDER>3</ORDER>
    <version_code>COMM</version_code>
</version>
<task_info_press_section>
    <ORDER>1</ORDER>
    <PRESS_x0020_SECTION>1</PRESS_x0020_SECTION>
    <task_sub_job_id>SC10268-001COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>4pp NEWS-NEWS COMM</task_description>
    <task_qty_rqd>9636</task_qty_rqd>
    <task_resource_id>1</task_resource_id>
    <task_mr_mins>120</task_mr_mins>
    <task_run_mins>240</task_run_mins>
    <task_run_speed>18000</task_run_speed>
    <task_notes>Task Notes</task_notes>
    <task_no_up>4</task_no_up>
    <task_deadline_date_time></task_deadline_date_time>
    <task_pdt>0.0</task_pdt>
</task_info_press_section>
<task_info_press_section>
    <ORDER>1</ORDER>
    <PRESS_x0020_SECTION>1</PRESS_x0020_SECTION>
    <task_sub_job_id>SC10268-1COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>Perfect Binding</task_description>
    <task_qty_rqd>19402</task_qty_rqd>
    <task_resource_id>1</task_resource_id>
    <task_mr_mins>120</task_mr_mins>
    <task_run_mins>240</task_run_mins>
    <task_run_speed>7500</task_run_speed>
    <task_notes>Task Notes</task_notes>
    <task_no_up>1</task_no_up>
    <task_deadline_date_time></task_deadline_date_time>
    <task_pdt>0.0</task_pdt>
</task_info_press_section>
<task_info_press_section>
    <ORDER>2</ORDER>
    <PRESS_x0020_SECTION>2</PRESS_x0020_SECTION>
    <task_sub_job_id>S019191-9-001COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>4pp Cover</task_description>
    <task_qty_rqd>1900</task_qty_rqd>
    <task_resource_id>2</task_resource_id>
    <task_mr_mins>20</task_mr_mins>
    <task_run_mins>63</task_run_mins>
    <task_run_speed>30</task_run_speed>
    <task_no_up>4</task_no_up>
    <task_pdt>0.0</task_pdt>
</task_info_press_section>
<task_info_press_section>
    <ORDER>2</ORDER>
    <PRESS_x0020_SECTION>2</PRESS_x0020_SECTION>
    <task_sub_job_id>S019191-9-1COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>Perfect Binding</task_description>
    <task_qty_rqd>1900</task_qty_rqd>
    <task_resource_id>2</task_resource_id>
    <task_mr_mins>20</task_mr_mins>
    <task_run_mins>120</task_run_mins>
    <task_run_speed>240</task_run_speed>
    <task_no_up>1</task_no_up>
    <task_pdt>0.0</task_pdt>
</task_info_press_section>
<task_info_press_section>
    <ORDER>3</ORDER>
    <PRESS_x0020_SECTION>3</PRESS_x0020_SECTION>
    <task_sub_job_id>S026500-1-001COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>4ppCover</task_description>
    <task_qty_rqd>20000</task_qty_rqd>
    <task_resource_id>2</task_resource_id>
</task_info_press_section>
<task_info_press_section>
    <ORDER>3</ORDER>
    <PRESS_x0020_SECTION>3</PRESS_x0020_SECTION>
    <task_sub_job_id>S026500-1-1COMM</task_sub_job_id>
    <task_seq_id>1</task_seq_id>
    <task_description>24ppText</task_description>
    <task_qty_rqd>20000</task_qty_rqd>
    <task_resource_id>2</task_resource_id>
</task_info_press_section>
<task_info_post_press>
    <ORDER>1</ORDER>
    <POST_x0020_PRESS>1</POST_x0020_PRESS>
    <task_sub_job_id>SC10268-1COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>Perfect Binding</task_description>
    <task_qty_rqd>19402</task_qty_rqd>
    <task_resource_id>10</task_resource_id>
    <task_mr_mins>120</task_mr_mins>
    <task_run_mins>240</task_run_mins>
    <task_run_speed>7500</task_run_speed>
    <task_notes>Task Notes</task_notes>
    <task_no_up>1</task_no_up>
    <task_deadline_date_time></task_deadline_date_time>
    <task_pdt>0.0</task_pdt>
</task_info_post_press>
<task_info_post_press>
    <ORDER>2</ORDER>
    <POST_x0020_PRESS>2</POST_x0020_PRESS>
    <task_sub_job_id>S019191-9-1COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>Perfect Binding</task_description>
    <task_qty_rqd>1900</task_qty_rqd>
    <task_resource_id>10</task_resource_id>
    <task_mr_mins>20</task_mr_mins>
    <task_run_mins>120</task_run_mins>
    <task_run_speed>240</task_run_speed>
    <task_no_up>1</task_no_up>
    <task_pdt>0.0</task_pdt>
</task_info_post_press>
<task_info_post_press>
    <ORDER>3</ORDER>
    <POST_x0020_PRESS>3</POST_x0020_PRESS>
    <task_sub_job_id>S026500-1-1COMM</task_sub_job_id>
    <task_seq_id>0</task_seq_id>
    <task_description>Perfect Binding</task_description>
    <task_qty_rqd>20000</task_qty_rqd>
    <task_resource_id>10</task_resource_id>
</task_info_post_press>
<post_press_version>
    <ORDER>1</ORDER>
    <post_press_version_op_id>0</post_press_version_op_id>
    <version_code>COMM</version_code>
    <post_press_resource_type>PB</post_press_resource_type>
</post_press_version>
<post_press_version>
    <ORDER>2</ORDER>
    <post_press_version_op_id>0</post_press_version_op_id>
    <version_code>COMM</version_code>
    <post_press_resource_type>PB</post_press_resource_type>
</post_press_version>
<post_press_version>
    <ORDER>3</ORDER>
    <post_press_version_op_id>0</post_press_version_op_id>
    <version_code>COMM</version_code>
    <post_press_resource_type>PB</post_press_resource_type>
</post_press_version>
</dataroot>

我的 XML 应该是这样的(为了方便查看重要部分,我删减了一些信息);

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata"
          generated="2015-01-07T14:06:55">
   <order job_id="" site_code="" replace="true">
      <job_description>TESTDATA</job_description>
      <order_qty>1900</order_qty>
      <finishing_style>PB</finishing_style>
      <depth>10</depth>
      <width>8</width>
      <cover_pagination>4</cover_pagination>
      <text_pagination>12</text_pagination>
      <delivery_commence_date>15/12/2014</delivery_commence_date>
      <delivery_complete_date>15/12/2014</delivery_complete_date>
      <job_site>DG</job_site>
      <managing_printer>DG</managing_printer>
      <is_managing_printer>TRUE</is_managing_printer>
      <cust_order_ref>776031</cust_order_ref>
      <cust_code>Test</cust_code>
      <site_cce_name>Jamie</site_cce_name>
      <site_cce_email>JamesBrace@dstoutput.co.uk</site_cce_email>
      <sales_person_name>Jamie Brace</sales_person_name>
      <sales_person_email>JamesBrace@dstouput.co.uk</sales_person_email>
      <master_version>
         <version_id/>
         <version_code>COMM</version_code>
         <version_common>true</version_common>
         <version_finished>false</version_finished>
         <version_description>Common</version_description>
         <version_nett_qty>176262</version_nett_qty>
         <version_special_qty>10</version_special_qty>
      </master_version>
   </order>
</dataroot>

但是看起来像这样;

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata"
          generated="2015-01-07T14:06:55">
   <order job_id="" site_code="" replace="true">
      <job_description>TESTDATA</job_description>
      <order_qty>1900</order_qty>
      <finishing_style>PB</finishing_style>
      <depth>10</depth>
      <width>8</width>
      <cover_pagination>4</cover_pagination>
      <text_pagination>12</text_pagination>
      <delivery_commence_date>15/12/2014</delivery_commence_date>
      <delivery_complete_date>15/12/2014</delivery_complete_date>
      <job_site>DG</job_site>
      <managing_printer>DG</managing_printer>
      <is_managing_printer>TRUE</is_managing_printer>
      <cust_order_ref>776031</cust_order_ref>
      <cust_code>Test</cust_code>
      <site_cce_name>Jamie</site_cce_name>
      <site_cce_email>JamesBrace@dstoutput.co.uk</site_cce_email>
      <sales_person_name>Jamie Brace</sales_person_name>
      <sales_person_email>JamesBrace@dstouput.co.uk</sales_person_email>
      <master_version>
         <ORDER>1</ORDER>
         <version_id/>
         <version_code>COMM</version_code>
         <version_common>true</version_common>
         <version_finished>false</version_finished>
         <version_description>Common</version_description>
         <version_nett_qty>176262</version_nett_qty>
         <version_special_qty>10</version_special_qty>
      </master_version>
      <master_version>
         <ORDER>2</ORDER>
         <version_code>COMM</version_code>
         <version_common>TRUE</version_common>
         <version_finished>FALSE</version_finished>
         <version_description>Common</version_description>
         <version_nett_qty>1900</version_nett_qty>
         <version_special_qty>0</version_special_qty>
      </master_version>
   </order>
</dataroot>

"master_version" 元素应该嵌套在转换之后,确实如此,但是如果 "master_version" 中的 "ORDER" 节点是,转换还告诉它删除所有元素不等于 "order" 中的 "ORDERPK",它似乎没有这样做。

有什么想法吗?

我还被告知我的 XSLT 格式不正确,有人可以帮忙吗?

您的 XSLT 存在许多问题。着眼于手头的问题,一个问题是您的 "order" 模板之一使用了 xsl:copy-of

 <xsl:template match="order">
    <order job_id="{@job_id}" site_code="{@site_code}" replace="{Replace}">
        <xsl:apply-templates select="node()"/>
        <xsl:copy-of select="../master_version"/>
     </order>
 </xsl:template>

在继续之前,您有两个匹配 "order" 的模板。严格来说,这被指定为 XSLT 中的错误。您实际上可能没有看到错误,因为某些处理器会忽略重复的模板,而只使用最后一个。你应该删除第一个。

无论如何,通过使用 xsl:copy-of 它只是将所有 master_version 元素复制到这里,而不管这里是否匹配任何其他模板。您需要使用 xsl:apply-templates

<xsl:template match="order">
   <order job_id="{@job_id}" site_code="{@site_code}" replace="{Replace}">
      <xsl:apply-templates select="node()"/>
      <xsl:apply-templates select="../master_version"/>
   </order>
</xsl:template>

但是由于这两个模板匹配,这本身是行不通的(尽管如前所述,必须同时匹配 master_version 的模板是错误的)

<xsl:template match="job_id | site_code | replace | master_version"/>

<xsl:template match="Replace | master_version"/>

从这些模板匹配中删除 master_version,只保留这个现有的

<xsl:template match="master_version[not(ORDER = //order/ORDERPK)]"/>

(请注意,当一个元素与某个条件匹配时,它实际上比刚刚匹配master_version的元素具有更高的优先级,因此在本例中这不是错误)。

此时你也会在那里,但你会发现现在master_version仍然会输出在他们当前的位置。为了解决这个问题,您可以有一个匹配 dataroot 的模板,并添加代码以在该点

显式忽略 master_version 元素
<xsl:template match="dataroot">
  <xsl:copy>
     <xsl:apply-templates select="@*|node()[not(self::master_version)]"/>
  </xsl:copy>
</xsl:template>

试试这个 XSLT。它可能不会给出您指定的精确输出,但它应该可以解决您询问的 master_version 元素的问题:

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

   <xsl:template match="master_version[not(ORDER = //order/ORDERPK)]"/>

   <xsl:template match="press_section[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="version[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="task_info_press_section[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="task_info_post_press[not(ORDER = //order/ORDERPK)]"/>
   <xsl:template match="post_press_version[not(ORDER = //order/ORDERPK)]"/>
   <!-- removes specified nodes from all elements -->

   <xsl:template match="ORDER"/>
   <xsl:template match="ORDERPK"/>
   <xsl:template match="PRESS_x0020_SECTION"/>
   <xsl:template match="POST_x0020_PRESS"/>

   <!-- Creates attributes against the ORDER element -->
   <xsl:strip-space elements="*"/>
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="dataroot">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()[not(self::master_version)]"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="job_id | site_code | replace | Replace"/>

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

   <xsl:template match="order">
      <order job_id="{@job_id}" site_code="{@site_code}" replace="{Replace}">
         <xsl:apply-templates select="node()"/>
         <xsl:apply-templates select="../master_version"/>
      </order>
   </xsl:template>

   <xsl:template match="task_info_press_section">
      <xsl:element name="task_info1">
         <xsl:apply-templates/>
      </xsl:element>
   </xsl:template>

   <xsl:template match="task_info_post_press">
      <xsl:element name="task_info2">
         <xsl:apply-templates/>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

另请注意,正如 Daniel Haley 在评论中提到的那样,多个身份模板已被删除。