EDIT;完全重写了这个问题。
感谢你所有的帮助到目前为止蒂姆,真的很感激它!
我复制了你发布的XSLT,它工作得非常好,谢谢你!这就是我目前正在努力解决的问题;
XSLT目前只在“order”元素中嵌套了“master_version”。我现在正试图以“顺序”嵌套其他元素,但是这就是导致问题的原因。
这是我当前的XML输入;
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-01-12T10:09:45">
<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>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"?>
<tg:orders xmlns:tg="http://www.technique-group.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.technique-group.com/schemas TGScheduleImport_v1.4.xsd" tg:version="1.2">
<tg:order job_id="S026500-1" site_code="DG" replace="true">
<tg:job_description>TESTING</tg:job_description>
<tg:order_qty>20000</tg:order_qty>
<tg:finishing_style>PB</tg:finishing_style>
<tg:depth>10</tg:depth>
<tg:width>8</tg:width>
<tg:cover_pagination>4</tg:cover_pagination>
<tg:text_pagination>24</tg:text_pagination>
<tg:delivery_commence_date>19/12/2014</tg:delivery_commence_date>
<tg:delivery_complete_date>19/12/2014</tg:delivery_complete_date>
<tg:job_site>DG</tg:job_site>
<tg:managing_printer>DG</tg:managing_printer>
<tg:is_managing_printer>True</tg:is_managing_printer>
<tg:master_version>
<tg:version_code>COMM</tg:version_code>
<tg:version_common>true</tg:version_common>
<tg:version_finished>false</tg:version_finished>
<tg:version_description>common</tg:version_description>
<tg:version_nett_qty>20000</tg:version_nett_qty>
<tg:version_special_qty>0</tg:version_special_qty>
</tg:master_version>
</tg:order>
<tg:press_section>
<tg:signature_id>001</tg:signature_id>
<tg:sequence_id>0</tg:sequence_id>
<tg:sequence_alpha>A</tg:sequence_alpha>
<tg:description>4pp Cover</tg:description>
<tg:pagination>4</tg:pagination>
<tg:trim_size>10 x 8</tg:trim_size>
</tg:press_section>
<tg:press_section>
<tg:signature_id>001</tg:signature_id>
<tg:sequence_id>1</tg:sequence_id>
<tg:sequence_alpha>A</tg:sequence_alpha>
<tg:description>24pp Text</tg:description>
<tg:pagination>24</tg:pagination>
<tg:trim_size>10 x 8</tg:trim_size>
</tg:press_section>
<tg:version>
<tg:version_code>COMM</tg:version_code>
</tg:version>
<tg:task_info1>
<tg:task_sub_job_id>S026500-1-001COMM</tg:task_sub_job_id>
<tg:task_seq_id>0</tg:task_seq_id>
<tg:task_description>4ppCover</tg:task_description>
<tg:task_qty_rqd>20000</tg:task_qty_rqd>
<tg:task_resource_id>2</tg:task_resource_id>
</tg:task_info1>
<tg:task_info1>
<tg:task_sub_job_id>S026500-1-1COMM</tg:task_sub_job_id>
<tg:task_seq_id>1</tg:task_seq_id>
<tg:task_description>24ppText</tg:task_description>
<tg:task_qty_rqd>20000</tg:task_qty_rqd>
<tg:task_resource_id>2</tg:task_resource_id>
</tg:task_info1>
<tg:task_info2>
<tg:task_sub_job_id>S026500-1-1COMM</tg:task_sub_job_id>
<tg:task_seq_id>0</tg:task_seq_id>
<tg:task_description>Perfect Binding</tg:task_description>
<tg:task_qty_rqd>20000</tg:task_qty_rqd>
<tg:task_resource_id>10</tg:task_resource_id>
</tg:task_info2>
<tg:post_press_version>
<tg:post_press_version_op_id>0</tg:post_press_version_op_id>
<tg:version_code>COMM</tg:version_code>
<tg:post_press_resource_type>PB</tg:post_press_resource_type>
</tg:post_press_version>
</tg:orders>
这是我想要的输出;
<?xml version="1.0" encoding="UTF-8"?>
<tg:orders xmlns:tg="http://www.technique-group.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.technique-group.com/schemas TGScheduleImport_v1.4.xsd" tg:version="1.2">
<tg:order job_id="S026500-1" site_code="DG" replace="true">
<tg:job_description>TESTING</tg:job_description>
<tg:order_qty>20000</tg:order_qty>
<tg:finishing_style>PB</tg:finishing_style>
<tg:depth>10</tg:depth>
<tg:width>8</tg:width>
<tg:cover_pagination>4</tg:cover_pagination>
<tg:text_pagination>24</tg:text_pagination>
<tg:delivery_commence_date>19/12/2014</tg:delivery_commence_date>
<tg:delivery_complete_date>19/12/2014</tg:delivery_complete_date>
<tg:job_site>DG</tg:job_site>
<tg:managing_printer>DG</tg:managing_printer>
<tg:is_managing_printer>True</tg:is_managing_printer>
<tg:master_version>
<tg:version_code>COMM</tg:version_code>
<tg:version_common>true</tg:version_common>
<tg:version_finished>false</tg:version_finished>
<tg:version_description>common</tg:version_description>
<tg:version_nett_qty>20000</tg:version_nett_qty>
<tg:version_special_qty>0</tg:version_special_qty>
</tg:master_version>
<tg:press_section>
<tg:signature_id>001</tg:signature_id>
<tg:sequence_id>0</tg:sequence_id>
<tg:sequence_alpha>A</tg:sequence_alpha>
<tg:description>4pp Cover</tg:description>
<tg:pagination>4</tg:pagination>
<tg:trim_size>10 x 8</tg:trim_size>
<tg:version>
<tg:version_code>COMM</tg:version_code>
<tg:task_info1>
<tg:task_sub_job_id>S026500-1-001COMM</tg:task_sub_job_id>
<tg:task_seq_id>0</tg:task_seq_id>
<tg:task_description>4ppCover</tg:task_description>
<tg:task_qty_rqd>20000</tg:task_qty_rqd>
<tg:task_resource_id>2</tg:task_resource_id>
</tg:task_info1>
</tg:version>
</tg:press_section>
<tg:press_section>
<tg:signature_id>001</tg:signature_id>
<tg:sequence_id>1</tg:sequence_id>
<tg:sequence_alpha>A</tg:sequence_alpha>
<tg:description>24pp Text</tg:description>
<tg:pagination>24</tg:pagination>
<tg:trim_size>10 x 8</tg:trim_size>
<tg:version>
<tg:version_code>COMM</tg:version_code>
<tg:task_info>
<tg:task_sub_job_id>S026500-1-1COMM</tg:task_sub_job_id>
<tg:task_seq_id>1</tg:task_seq_id>
<tg:task_description>24ppText</tg:task_description>
<tg:task_qty_rqd>20000</tg:task_qty_rqd>
<tg:task_resource_id>2</tg:task_resource_id>
</tg:task_info>
</tg:version>
</tg:press_section>
<tg:post_press_version>
<tg:post_press_version_op_id>0</tg:post_press_version_op_id>
<tg:version_code>COMM</tg:version_code>
<tg:post_press_resource_type>PB</tg:post_press_resource_type>
<tg:task_info2>
<tg:task_sub_job_id>S026500-1-1COMM</tg:task_sub_job_id>
<tg:task_seq_id>0</tg:task_seq_id>
<tg:task_description>Perfect Binding</tg:task_description>
<tg:task_qty_rqd>20000</tg:task_qty_rqd>
<tg:task_resource_id>10</tg:task_resource_id>
</tg:task_info2>
</tg:post_press_version>
</tg:order>
</tg:orders>
我有2个XSLT,一个是格式化我需要的XML,另一个是格式化完成后应用于整个XML文件的命名空间声明;
这是XSLT1;
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- creates new root element -->
<xsl:template match="/">
<orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tg="http://www.technique-group.com" xsi:schemaLocation="http://www.technique-group.com/schemas TGScheduleImport_v1.4.xsd" tg:version="1.2">
<xsl:apply-templates/>
</orders>
</xsl:template>
<!-- Commented out as this template occurs later -->
<!--
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
-->
<!-- matches the template saying remove elements if not matching the order element -->
<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"/>
<!-- CHANGED "order" to "dataroot" -->
<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()"/>
<!-- nests elements within order -->
<xsl:apply-templates select="../master_version"/>
</order>
</xsl:template>
<!-- renames elements to desired name -->
<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>
这是XSLT2;
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tg="http://www.technique-group.com"
version="1.2">
<xsl:template match="*">
<xsl:element name="tg:{local-name()}" namespace="http://www.technique-group.com">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="dataroot">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
基本上,我想将我的元素嵌套到以下结构中;
<orders>
<order>
<info>1</info>
<master_version>
<info>1</info>
</master_version>
<press_section>
<info>1</info>
<version>
<info>1</info>
<task_info_press_section>
<info>1</info>
</task_info_press_section>
</version>
</press_section>
<post_press_version
<info>1</info>
<task_info_post_press>
<info>1</info>
</task_info_post_press>
</post_press_version>
</order>
</orders>
答案 0 :(得分:1)
如果您希望将所有其他元素嵌套在order
下,首先要做的是更改xsl:apply-templates
匹配模板中的现有dataroot
....
<xsl:apply-templates select="@*|node()[not(self::master_version)]"/>
对此......
<xsl:apply-templates select="@*|node()[not(self::*)]|order"/>
这将复制所有属性,以及所有非元素的节点和order
元素。
(注意,您可以将其简化为<xsl:apply-templates select="@*|*[not(self::order)]"/>
,但这不会选择文本节点,注释或处理指令,这些都是&#34;节点&#34;)
然后,在匹配order
的模板中,更改现有的xsl:apply-templates
<xsl:apply-templates select="../master_version"/>
为此,它将选择所有不是order
元素
<xsl:apply-templates select="../*[not(self::order)]"/>
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tg="http://www.technique-group.com" xsi:schemaLocation="http://www.technique-group.com/schemas TGScheduleImport_v1.4.xsd" tg:version="1.2">
<xsl:apply-templates/>
</orders>
</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)]"/>
<xsl:template match="ORDER"/>
<xsl:template match="ORDERPK"/>
<xsl:template match="PRESS_x0020_SECTION"/>
<xsl:template match="POST_x0020_PRESS"/>
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="dataroot">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::*)]|order"/>
</xsl:copy>
</xsl:template>
<xsl:template match="job_id | site_code | replace | Replace"/>
<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="../*[not(self::order)]"/>
</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>
请注意,在您的第二个XSLT中,我注意到您正在删除dataroot
元素。可能值得在第一个XSLT中执行该操作,因为这会使您的第二个XSLT更通用。只需将第一个XSLT中的现有dataroot
模板更改为此...
<xsl:template match="dataroot">
<xsl:apply-templates select="@*|node()[not(self::*)]|order"/>
</xsl:template>