XSLT 删除 XML 的元素
XSLT To Remove Elements of an XML
我有以下 XML;
<?xml version="1.0"?>
<dataroot generated="2015-01-07T10:49:16" xmlns:od="urn:schemas-microsoft-com:officedata">
<order job_id="S026500-1" site_code="DG" replace="">
<ORDERPK>3</ORDERPK>
<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>
我想删除 "order" table 中订单不等于 ORDERPK 的所有 "master_version" table。有没有人可以为此提供 XSLT 帮助我?我没有 post 的任何代码,我完全不知道如何做到这一点。
谢谢!
如果您从模板开始
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
您可以添加模板来删除节点,例如
<xsl:template match="master_version[not(ORDER = //order/ORDERPK)]"/>
将删除在 order/ORDERPK
元素中找不到 ORDER
元素值的 master_version
元素。
使用身份模板 return 一切保持不变,并为 <master_version>
元素提供覆盖模板以仅 return 匹配顺序:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<!-- - - - - - - - - - - - - - - - - - - - - - - -
IDENTITY
- - - - - - - - - - - - - - - - - - - - - - - - -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- - - - - - - - - - - - - - - - - - - - - - - -
OVERRIDES
- - - - - - - - - - - - - - - - - - - - - - - - -->
<xsl:template match="master_version">
<xsl:if test="ORDER = /dataroot/order/ORDERPK">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
我有以下 XML;
<?xml version="1.0"?>
<dataroot generated="2015-01-07T10:49:16" xmlns:od="urn:schemas-microsoft-com:officedata">
<order job_id="S026500-1" site_code="DG" replace="">
<ORDERPK>3</ORDERPK>
<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>
我想删除 "order" table 中订单不等于 ORDERPK 的所有 "master_version" table。有没有人可以为此提供 XSLT 帮助我?我没有 post 的任何代码,我完全不知道如何做到这一点。
谢谢!
如果您从模板开始
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
您可以添加模板来删除节点,例如
<xsl:template match="master_version[not(ORDER = //order/ORDERPK)]"/>
将删除在 order/ORDERPK
元素中找不到 ORDER
元素值的 master_version
元素。
使用身份模板 return 一切保持不变,并为 <master_version>
元素提供覆盖模板以仅 return 匹配顺序:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<!-- - - - - - - - - - - - - - - - - - - - - - - -
IDENTITY
- - - - - - - - - - - - - - - - - - - - - - - - -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- - - - - - - - - - - - - - - - - - - - - - - -
OVERRIDES
- - - - - - - - - - - - - - - - - - - - - - - - -->
<xsl:template match="master_version">
<xsl:if test="ORDER = /dataroot/order/ORDERPK">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>