[英]Update a XML file and assigning an id to a tag using XSLT
我想更新一個 XML 文件並想為標簽分配一個 ID。 在我的 XML 文件中,有一個名為“ComponentDetails”的標記,因為有多個具有相同名稱的標記,所以我想為其分配一個 id,以便我可以根據 ID 選擇特定值。
以下是我的 XML 示例:
<?xml version="1.0" encoding="UTF-8"?><root>
<PayrunDetails>
<PayrunNumber>000777</PayrunNumber>
<PaidDate>2018-05-15</PaidDate>
</PayrunDetails>
<PayLocation>
<LocationCode>ACT</LocationCode>
<LocationDescription>ACT</LocationDescription>
<CompanyDetails>
<CCode>APPLE</CCode>
<CName>APPLE Limited</CName>
<Payslip>
<StaffNumber>12345</StaffNumber>
<BankDetails>
<BankAccountNo>121212</BankAccountNo>
</BankDetails>
<PayDetails>
<PayType>NORMAL</PayType>
<AmountGross>9999</AmountGross>
<ComponentDetails>
<ComponentType>SALARY</ComponentType>
<Amount>1999</Amount>
<YTDAmount>10616</YTDAmount>
</ComponentDetails>
<ComponentDetails>
<ComponentType>TAXABLE</ComponentType>
<Amount>505</Amount>
<YTDAmount>7703</YTDAmount>
</ComponentDetails>
</PayDetails>
</Payslip>
<Payslip>
<StaffNumber>45555</StaffNumber>
<BankDetails>
<BankAccountNo>131313</BankAccountNo>
</BankDetails>
<PayDetails>
<PayType>NORMAL</PayType>
<AmountGross>9999</AmountGross>
<ComponentDetails>
<ComponentType>SALARY</ComponentType>
<Amount>1999</Amount>
<YTDAmount>10616</YTDAmount>
</ComponentDetails>
<ComponentDetails>
<ComponentType>GROSS</ComponentType>
<Amount>7305</Amount>
<YTDAmount>76703</YTDAmount>
</ComponentDetails>
<ComponentDetails>
<ComponentType>TAXABLE</ComponentType>
<Amount>305</Amount>
<YTDAmount>6703</YTDAmount>
</ComponentDetails>
</PayDetails>
</Payslip>
</CompanyDetails>
</PayLocation>
</root>
從上面的 XML 文件中,我想選擇 ComponentDetails 標簽中存在的 Amount、YTDAmount 子標簽值。 不知何故,我設法為 ComponentDetails 標簽分配了一個 id,但不幸的是,其中存在新創建的 XML 文件中缺少的子標簽。
我的 XSL 文件如下:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ComponentDetails">
<xsl:copy>
<xsl:attribute name="ID">
<xsl:number/>
</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
所以請為我提供創建帶有標簽 ID 的 XML 文件的解決方案。 我對 XSLT 還很陌生,所以請原諒潛在的新手問題。 任何指導將不勝感激。
提前致謝。
我做了以下更改並且它有效:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="PayDetails/ComponentDetails">
<xsl:copy>
<xsl:attribute name="ID">
<xsl:number/>
</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
你必須擺脫這條線
<xsl:template match="ComponentDetails/node()"/>
因為它刪除了ComponentDetails
元素的所有子標簽。
此外,如果您想要一個全局唯一ID
,請將<xsl:number/>
元素更改為
<xsl:number level="any" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.