代码之家  ›  专栏  ›  技术社区  ›  venus

使用XSLT更新XML文件并为标记分配id

  •  1
  • venus  · 技术社区  · 6 年前

    我想更新一个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>
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   zx485 potemkin    6 年前

    你必须摆脱这条线

    <xsl:template match="ComponentDetails/node()"/>
    

    因为它删除了 ComponentDetails 要素


    此外,如果您希望全局唯一 ID ,更改 <xsl:number/> 元素到

    <xsl:number level="any" />