我有一個需要用 XSL 處理的 XLM,我的代碼將幾個元素連接到描述元素,但輸出只需要包含已更改的元素。
現在,我的代碼回傳正確連接的所有內容,但它也回傳不需要任何更改的元素。
我的示例 XML 有 3 項,第一項沒有任何更改,因此不需要回傳。第二個在數量上有變化,第三個需要連接部分,所以最后兩個是唯一應該回傳的部分。
示例 XML
<?xml version='1.0' encoding='UTF-8'?>
<document>
<myObjects>
<Base>
<Code>11</Code>
<Item>216</Item>
<Build>BH</Build>
<Quantity>203</Quantity>
<Description>BH-203-216|Description 1</Description>
</Base>
<Base>
<Code>19</Code>
<Item>169</Item>
<Build>FM</Build>
<Quantity>2</Quantity>
<Description>FM-201-169|Description 2</Description>
</Base>
<Base>
<Code>20</Code>
<Item>169</Item>
<Build>MM</Build>
<Quantity></Quantity>
<Description>Description 3</Description>
</Base>
</myObjects>
</document>
生成的 XML 應如下所示:
<document>
<myObjects>
<Base>
<Code>19</Code>
<Description>FM-2-169|Description 2</Description>
</Base>
<Base>
<Code>20</Code>
<Description>MM-169|Description 3</Description>
</Base>
</myObjects>
</document>
我的 XSL 代碼:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="utf-8" media-type="xml/plain"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- drop elements with concat-->
<xsl:template match="Base">
<xsl:variable name="currentPre" select="substring-before(Description,'|')" />
<xsl:variable name="currentPost" select="substring-after(Description,'|')" />
<xsl:variable name="NewPre" select="string-join((Build,Quantity/text(),Item/text()), '-')" />
<xsl:copy>
<xsl:choose>
<xsl:when test="not(contains(Description, '|'))">
<xsl:variable name="emptyDes" select="Description" />
<xsl:copy-of select="Code"/>
<Description>
<xsl:value-of select="string-join((Build,Quantity/text(),Item/text()), '-'), Description" separator="|"/>
</Description>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="Code"/>
<Description>
<xsl:value-of select="string-join((Build,Quantity/text(),Item/text()), '-'), $currentPost" separator="|"/>
</Description>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
uj5u.com熱心網友回復:
如果你測驗新構造的Description值是否等于當前Description值,并且只有在不同時才復制,你還可以合并生成這些專案的邏輯:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="utf-8" media-type="xml/plain"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- drop elements with concat-->
<xsl:template match="Base">
<xsl:variable name="currentPre" select="substring-before(Description,'|')" as="xs:string"/>
<xsl:variable name="currentPost" select="substring-after(Description,'|')[normalize-space()]" as="xs:string?" />
<xsl:variable name="NewPost" select="($currentPost,Description)[1]"/>
<xsl:variable name="NewPre" select="string-join((Build,Quantity/text(),Item/text()), '-')" />
<xsl:variable name="NewDescription" select="string-join(($NewPre, $NewPost), '|')"/>
<xsl:if test="not(Description eq $NewDescription)">
<xsl:copy>
<xsl:copy-of select="Code"/>
<Description><xsl:value-of select="$NewDescription"/></Description>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468543.html
上一篇:在javascript中使用.reduce從每個特定用戶的物件陣列中獲取總價
下一篇:由于底部導航,沒有看到浮動按鈕
