XSLT根据它的名称XML来嵌套元素

时间:2015-01-07 12:39:35

标签: xml xslt nested transform

我希望根据原始元素的名称将元素嵌套在另一个元素中。

这是我目前的XML;

<order job_id="S026500-1" site_code="DG" replace="">
<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>
<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>

“master_version”需要嵌套在“orders”中,但XML中还有其他不希望嵌套的元素,那么我如何有条件地嵌套“master_version”?

我已经用谷歌搜索了一段时间了,我似乎找不到任何适用于我的东西。我是使用XML和XSLT的新手,所以我可能还没有理解它。

感谢你们的帮助!

另外作为附注,我当前的XSLT将节点“Replace”更改为“order”的属性,但是它如上所述?以下是XSLT的一部分;

<!-- 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"/>

编辑:

我已经尝试过这个XSLT,不幸的是它看起来并没有什么不同;

<xsl:template match="order">
    <order job_id="{job_id}" site_code="{site_code}" replace="{Replace}">
        <xsl:apply-templates select="node()[not(self::job_id|self::site_code|self::replace)"/> 
        <xsl:copy-of select="following-sibling::master_version[1]"/> 
    </order>
</xsl:template>

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

我希望输出看起来像这样;

<order job_id="S026500-1" site_code="DG" replace="">
<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>
<master_version>
<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>
</order>

再次编辑;

现在我已经解决了上述问题,我的XML现在正在这样做..

我有以下XSLT;

<?xml version="1.0" encoding="UTF-8"?>

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

<!-- 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"?>
<dataroot generated="2015-01-07T14:06:55" xmlns:od="urn:schemas-microsoft-com:officedata">
<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_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>

但它看起来像这样;

<?xml version="1.0"?>
<dataroot generated="2015-01-07T14:06:55" xmlns:od="urn:schemas-microsoft-com:officedata">
<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_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>

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

如果你改变了

<xsl:template match="order">
    <order job_id="{job_id}" site_code="{site_code}" replace="{replace}">
        <xsl:apply-templates/>  
    </order>
</xsl:template>

<xsl:template match="order">
    <order job_id="{job_id}" site_code="{site_code}" replace="{replace}">
        <xsl:apply-templates select="node()[not(self::job_id|self::site_code|self::replace)"/> 
        <xsl:copy-of select="following-sibling::master_version[1]"/> 
    </order>
</xsl:template>

然后添加<xsl:template match="master_version"/>您将获得嵌套,假设它应该针对以下兄弟master_version元素而不是基于某个参考值。

答案 1 :(得分:0)

给出格式良好的(!)输入

<root>
  <order job_id="S026500-1" site_code="DG" replace="">
    <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>
    <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>
</root>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- 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"/>

</xsl:stylesheet>

将返回:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <order job_id="S026500-1" site_code="DG" replace="true">
    <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>
    <master_version>
      <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>
  </order>
</root>