我正在嘗試使用 XSL 將幾個 XML 欄位連接到另一個 XML 欄位中。
我想獲取一個包含多個元素的 XML,其中一些元素我想根據某些標準連接在一起,并將新連接的字串添加到其中一個元素。
串聯應遵循以下模式:
如果所有元素都在那里:build-Space-Item|Description
如果沒有 Space 資料:build-Item|Description
如果沒有專案:build-Space|Description
如果沒有空格也沒有專案:build|Description
我對 XSLT 非常陌生,并且在嘗試找到正確的語法來創建邏輯時磕磕絆絆。
我的 XML 如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<document>
<businessobjects>
<BaseOrder>
<Code>1190.00</Code>
<Item>VE216</Item>
<buil>CORP</buil>
<Space></Space>
<Type>Request</Type>
<Description>Description 1</Description>
</BaseOrder>
<BaseOrder>
<Code>64497.00</Code>
<Item>HP01</Item>
<buil>FO3</buil>
<Space>002</Space>
<Type>PPM</Type>
<Description>Description 2</Description>
</BaseOrder>
<BaseOrder>
<Code>77793.00</Code>
<Item></Item>
<buil>EN4</buil>
<Space>123</Space>
<Type>Request</Type>
<Description>Description 3</Description>
</BaseOrder>
<BaseOrder>
<Code>70796.00</Code>
<Item></Item>
<buil>SHS</buil>
<Space></Space>
<Type>Event</Type>
<Description>Description 4</Description>
</BaseOrder>
</businessobjects>
</document>
我的輸出 XML 應該如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<document>
<businessobjects>
<BaseOrder>
<Code>1190.00</Code>
<Description>CORP-VE216|Description 1</Description>
</BaseOrder>
<BaseOrder>
<Code>64497.00</Code>
<Description>FO3-002-HP01|Description 2</Description>
</BaseOrder>
<BaseOrder>
<Code>77793.00</Code>
<Description>EN4-123|Description 3</Description>
</BaseOrder>
<BaseOrder>
<Code>70796.00</Code>
<Description>SHS|Description 4</Description>
</BaseOrder>
</businessobjects>
</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 smart (kinda..) concat-->
<xsl:template match="BaseOrder">
<BaseOrder>
<xsl:variable name="noSpace" select="concat(build,'-',Item,'|')" />
<xsl:variable name="noItem" select="concat(build,'-',Space,'|')" />
<xsl:variable name="noSpaceItem" select="concat(build,'|')" />
<xsl:variable name="full" select="concat(build,'-',Space,'-',Item,'|')" />
<xsl:choose>
<!-- no space -->
<xsl:when test="not(@Space)">
<Code>
<xsl:value-of select="Code"/>
</Code>
<Description>
<xsl:value-of select="concat($noSpace,Description)"/>
</Description>
</xsl:when>
<!-- no Item -->
<xsl:when test="not(@Item)">
<Code>
<xsl:value-of select="Code"/>
</Code>
<Description>
<xsl:value-of select="concat($noItem,Description)"/>
</Description>
</xsl:when>
<!-- no Space or Item -->
<xsl:when test="not(@Space) and not(@Item)">
<Code>
<xsl:value-of select="Code"/>
</Code>
<Description>
<xsl:value-of select="concat($noSpaceItem,Description)"/>
</Description>
</xsl:when>
<!-- When all -->
<xsl:otherwise>
<Code>
<xsl:value-of select="Code"/>
</Code>
<Description>
<xsl:value-of select="concat($full,Description)"/>
</Description>
</xsl:otherwise>
</xsl:choose>
</BaseOrder>
</xsl:template>
</xsl:stylesheet>
編輯:元素不是屬性..
uj5u.com熱心網友回復:
簡單地說:
XSLT 2.0
<xsl:stylesheet version="2.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="BaseOrder">
<xsl:copy>
<xsl:copy-of select="Code"/>
<Description>
<xsl:value-of select="buil, Space[text()], Item[text()]" separator="-"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="Description"/>
</Description>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
或者,如果您愿意:
<xsl:template match="BaseOrder">
<xsl:copy>
<xsl:copy-of select="Code"/>
<Description>
<xsl:value-of select="string-join((buil, Space/text(), Item/text()), '-'), Description" separator="|"/>
</Description>
</xsl:copy>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466240.html
