2015-01-07 3 views
-4

У меня небольшая проблема с преобразованием XSLT.Преобразование 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>[email protected]</site_cce_email> 
     <sales_person_name>Jamie Brace</sales_person_name> 
     <sales_person_email>[email protected]</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>[email protected]</site_cce_email> 
     <sales_person_name>Jamie Brace</sales_person_name> 
     <sales_person_email>[email protected]</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» должны быть вложены после преобразования, которые она, но преобразование также говорит это, чтобы удалить все элементы, если «ORDER» узел «master_version» не равно " ORDERPK "в" порядке ", который он, кажется, не делает.

Любые идеи?

Также мне сказали, что мой XSLT не отформатирован очень хорошо, может ли кто-нибудь помочь с этим?

+6

Я переформатировал ваш XSLT-код. Для ваших будущих вопросов лучше всего оцените результат, например, с помощью http://codebeautify.org/xmlviewer. Кроме того, отредактируйте свою запись и включите входной документ XML. И, что очень важно: не просто бросайте два XML-вывода у нас, _explain_ как они разные. –

+2

Ваши результаты XML были искажены - перед отправкой вопроса, пожалуйста, проверьте корректность. Кроме того, ваше название вопроса очень бесполезно. Подумайте о чем-то более информативном, чем «преобразование не работает». –

+1

SO не является бесплатной службой отладки. Такие вопросы не подходят. –

ответ

7

С XSLT возникает ряд проблем. Сосредоточение внимания на проблемы в руке, один вопрос в один из ваших шаблонов «заказ» использует 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> 

Прежде чем идти дальше, хотя, у вас есть два шаблона согласования «заказ». Это, строго говоря, указано как ошибка в 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)]"/> 

(Обратите внимание, что, когда эль ent соответствует условию, на самом деле он будет иметь более высокий приоритет, чем тот, который соответствует только master_version, так что это не ошибка в этом случае).

Вы также были бы здесь на данный момент, но теперь вы найдете, что master_version все равно будет выводиться и в их текущем месте. Чтобы обойти эту проблему, вы можете иметь шаблон, соответствующий dataroot и добавить код, чтобы явно игнорировать в этой точке

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

в master_version элементы Попробуйте 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> 

Также обратите внимание на несколько шаблонов идентичности были удалены, как было упомянуто Даниэль Хейли в комментариях.

+2

Вы заслуживаете -100 для поощрения таких плохих вопросов, но +101 для ваших усилий и сострадания. – kjhughes

+2

Во-вторых, вы только что заработали _Nicest XSLT Person 2015_ - снова. –

+0

Да, это очень много, спасибо вам большое! У меня все еще есть проблема с атрибутами, которые не применяются к «заказу», но я ценю это, спасибо! Прошу прощения, если мои вопросы не до нуля, я новичок в XSLT, и мне бы хотелось получить шанс. –

Смежные вопросы