如何在代码中获取节点的结果?

时间:2019-06-25 09:38:43

标签: xml xslt xslt-1.0

我有一个XML发票,节点PeriodWeekNum在其中。我想要年份,后跟字母W,然后是周号-> YYYNWW。我只得到年份和字母w-> 2019W。我想在代码中也包含节点PeriodWeekNum的结果,但似乎无法获取。

我已经尝试了以下方法:

<xsl:template match="/">
   <xsl:for-each select="/x:Invoice">
      <xsl:if test="x:Header/x:DocumentDateTime"><xsl:value-of select="concat(substring(x:Header/x:DocumentDateTime,1,4),'W')"/></xsl:if>
      <xsl:if test="x:Line/x:UserArea/x:TimeCard/x:ReportedTime/x:PeriodWeekNum"><xsl:value-of select="x:Line/x:UserArea/x:TimeCard/x:ReportedTime/x:PeriodWeekNum"/></xsl:if>
   </xsl:for-each>
</xsl:template>

这是xml发票

<Invoice xmlns="http://www.openapplications.org/oagis" xsi="http://www.w3.org/2001/XMLSchema-instance" hrx="http://ns.hr-xml.org/2007-04-15" nl="http://ns.hr-xml.org/2007-04-15" schemaLocation="http://www.openapplications.org/oagis ../Invoice.xsd http://ns.hr-xml.org/2007-04-15 InvoiceAdditionalNL.xsd">
<Header>
<DocumentIds>
<DocumentId>
<Id>19068863</Id>
</DocumentId>
</DocumentIds>
<DocumentDateTime>2019-06-18T00:00:00Z</DocumentDateTime>
<ReasonCode>hours</ReasonCode>
<TotalAmount currency="EUR">58.02</TotalAmount>
<PaymentTerms>
<PaymentTermsDate>2019-06-18T00:00:00Z</PaymentTermsDate>
<NumberOfDays>40</NumberOfDays>
</PaymentTerms>
<Parties>
<BillToParty>
<PartyId>
<Id>012634</Id>
</PartyId>
<Name>BLABLABLA</Name>
<Addresses>
<PrimaryAddress>
<AddressLine>BLABLA 17</AddressLine>
<City>BLABLA</City>
<Country>NL</Country>
<PostalCode>2341 DA</PostalCode>
<Telephone>0123435</Telephone>
</PrimaryAddress>
</Addresses>
<Contacts>
<Contact>
<Description>bladieblad</Description>
</Contact>
</Contacts>
</BillToParty>
<CustomerParty>
<PartyId>
<Id>0123333</Id>
</PartyId>
<Name>Aasdsdasd</Name>
<Addresses>
<PrimaryAddress>
<AddressLine>Nasdasd 44</AddressLine>
<City>REasdsad</City>
<Country>NL</Country>
<PostalCode>1111 AA/PostalCode>
<Telephone>01234344545</Telephone>
</PrimaryAddress>
</Addresses>
<Contacts>
<Contact>
<Description>9008</Description>
</Contact>
</Contacts>
</CustomerParty>
<RemitToParty>
<PartyId>
<Id>8</Id>
</PartyId>
<Name>bladieblad BV</Name>
<TaxId>122323421</TaxId>
<Addresses>
<PrimaryAddress>
<AddressLine>Mercudfsdfsdf 1</AddressLine>
<City>Hsdfsdfds</City>
<PostalCode>1111 DD</PostalCode>
<Description>Hertertertntoor</Description>
<Telephone>12344545-50</Telephone>
</PrimaryAddress>
<SecondaryAddress/>
</Addresses>
<Contacts>
<Contact>
<Description>Addfwefw</Description>
<Addresses>
<PrimaryAddress>
<AddressLine>Pwefwefwe 24</AddressLine>
<City>HEwefwefe</City>
<Country>NL</Country>
<PostalCode>1111 DT</PostalCode>
<Description>Vestiging</Description>
<Telephone>121212</Telephone>
</PrimaryAddress>
<SecondaryAddress>
<AddressLine>Pefefefe</AddressLine>
<City>HEfefe</City>
<Country>NL</Country>
<PostalCode>61212 df</PostalCode>
</SecondaryAddress>
</Addresses>
</Contact>
</Contacts>
</RemitToParty>
<SupplierParty>
<PartyId>
<Id>8</Id>
</PartyId>
<Name>Oereere3r</Name>
<TaxId>ererere44B01</TaxId>
<Addresses>
<PrimaryAddress>
<AddressLine>Mefefegfefe 1</AddressLine>
<City>HOefefefe</City>
<PostalCode>2111 HA</PostalCode>
<Description>H3r344343</Description>
<Telephone>0122212120</Telephone>
</PrimaryAddress>
<SecondaryAddress/>
</Addresses>
<Contacts>
<Contact>
<Description>blabla</Description>
<Addresses>
<PrimaryAddress>
<AddressLine>ererer 24</AddressLine>
<City>HererN</City>
<Country>NL</Country>
<PostalCode>6111 LT</PostalCode>
<Description>Ve34343stg</Description>
<Telephone>026 47errer5</Telephone>
</PrimaryAddress>
<SecondaryAddress>
<AddressLine>Pooererer 24</AddressLine>
<City>Hererere</City>
<Country>NL</Country>
<PostalCode>6611 dd</PostalCode>
</SecondaryAddress>
</Addresses>
</Contact>
</Contacts>
</SupplierParty>
</Parties>
<Type>Both</Type>
<Reason>regular</Reason>
<TotalCharges currency="EUR">47.95</TotalCharges>
<TotalTax currency="EUR">10.07</TotalTax>
<Tax>
<TaxAmount currency="EUR">10.07</TaxAmount>
<TaxBaseAmount currency="EUR">47.95</TaxBaseAmount>
<PercentQuantity uom="BTW">21.00</PercentQuantity>
</Tax>
<UserArea>
<StaffingAdditionalData xmlns="http://ns.hr-xml.org/2007-04-15">
<CustomerReportingRequirements>
<PurchaseOrderNumber/>
<CostCenterCode>9523</CostCenterCode>
</CustomerReportingRequirements>
<ReferenceInformation>
<StaffingSupplierId idOwner="StaffingCustomer">
<IdValue>8</IdValue>
</StaffingSupplierId>
<StaffingCustomerId idOwner="StaffingCustomer">
<IdValue>012634</IdValue>
</StaffingCustomerId>
<StaffingCustomerOrgUnitId idOwner="StaffingCustomer">
<IdValue name="Ablabla"/>
</StaffingCustomerOrgUnitId>
</ReferenceInformation>
</StaffingAdditionalData>
<StaffingOrganization typeOfOrganization="Supplier" xmlns="http://ns.hr-xml.org/2007-04-15">
<Organization>
<OrganizationName>Oldedede</OrganizationName>
</Organization>
<PaymentInfo>
<BankAccountInfo>
<BankInfoByJurisdiction bankJurisdiction="NL">
<BankCode>BANKBANK</BankCode>
<BankWindow/>
<BankAccountNumber>NLBDEUBDUEBDEUBD</BankAccountNumber>
<BankAccountKey/>
<BankAccountType>IBAN</BankAccountType>
</BankInfoByJurisdiction>
<BankAccountForeign/>
</BankAccountInfo>
</PaymentInfo>
</StaffingOrganization>
<StaffingOrganizationNL xmlns="http://ns.hr-xml.org/2007-04-15">
<ChamberofCommerceReference>27ededed</ChamberofCommerceReference>
</StaffingOrganizationNL>
</UserArea>
</Header>
<Line>
<LineNumber>1</LineNumber>
<ReasonCode/>
<Charges>
<TotalCharge>
<Total currency="EUR">-815.60</Total>
</TotalCharge>
</Charges>
<DocumentReferences>
<InvoiceDocumentReference>
<DocumentIds>
<DocumentId>
<Id>1edede</Id>
</DocumentId>
</DocumentIds>
</InvoiceDocumentReference>
</DocumentReferences>
<Line>
<LineNumber>1A</LineNumber>
<Charges>
<TotalCharge>
<Total currency="EUR">-775.60</Total>
</TotalCharge>
</Charges>
<Tax>
<PercentQuantity uom="BTW">21.00</PercentQuantity>
</Tax>
<Price>
<Amount currency="EUR">22.16</Amount>
<PerQuantity uom="regular">1</PerQuantity>
</Price>
<ItemQuantity uom="regular">-35.00</ItemQuantity>
</Line>
<Line>
<LineNumber>1B</LineNumber>
<Charges>
<TotalCharge>
<Total currency="EUR">-40.00</Total>
</TotalCharge>
</Charges>
<Tax>
<PercentQuantity uom="BTW">21.00</PercentQuantity>
</Tax>
<Price>
<Amount currency="EUR">8.00</Amount>
<PerQuantity uom="Travel expenses">1</PerQuantity>
</Price>
<ItemQuantity uom="Travel expenses">-5.00</ItemQuantity>
</Line>
<UserArea>
<TimeCard xmlns="http://ns.hr-xml.org/2007-04-15">
<Id idOwner="StaffingCustomer">
<IdValue>Aedede</IdValue>
</Id>
<ReportedResource>
<Person>
<Id idOwner="StaffingCustomer">
<IdValue>151MIET001</IdValue>
</Id>
<PersonName>
<FormattedName>M. edededed</FormattedName>
<LegalName>M. Mdededede</LegalName>
<GivenName>Maededed</GivenName>
<PreferredGivenName>Mdedede</PreferredGivenName>
<FamilyName>Miededed</FamilyName>
</PersonName>
</Person>
</ReportedResource>
<ReportedTime>
<PeriodStartDate>2019-05-20T00:00:00Z</PeriodStartDate>
<PeriodEndDate>2019-05-26T00:00:00Z</PeriodEndDate>
<PeriodWeekNum>21</PeriodWeekNum>
<TimeInterval type="regular" billable="true">
<StartDateTime>2019-05-20T00:00:00Z</StartDateTime>
<EndDateTime>2019-05-26T00:00:00Z</EndDateTime>
<Duration>35.00</Duration>
<RateOrAmount currency="EUR" type="Hourly" multiplier="100.00">22.16</RateOrAmount>
<AdditionalData>
<CustomerReportingRequirements>
<PurchaseOrderLineItem>0</PurchaseOrderLineItem>
</CustomerReportingRequirements>
</AdditionalData>
</TimeInterval>
<Allowance type="Travel expenses" billable="true">
<Id>
<IdValue name="allowance"/>
</Id>
<StartDate>2019-05-20T00:00:00Z</StartDate>
<EndDate>2019-05-26T00:00:00Z</EndDate>
<Amount currency="EUR">8.00</Amount>
<Quantity>5.00</Quantity>
<AdditionalData>
<CustomerReportingRequirements>
<PurchaseOrderLineItem>0</PurchaseOrderLineItem>
</CustomerReportingRequirements>
</AdditionalData>
</Allowance>
</ReportedTime>
<SubmitterInfo>
<SubmittedDateTime>2019-06-19</SubmittedDateTime>
</SubmitterInfo>
<AdditionalData>
<StaffingAdditionalData xmlns="http://ns.hr-xml.org/2007-04-15">
<CustomerReportingRequirements>
<PurchaseOrderNumber/>
<CostCenterCode>9523</CostCenterCode>
<ProjectCode/>
</CustomerReportingRequirements>
<ReferenceInformation>
<AssignmentId idOwner="StaffingCompany">
<IdValue>1510009388</IdValue>
</AssignmentId>
</ReferenceInformation>
</StaffingAdditionalData>
</AdditionalData>
</TimeCard>
<StaffingAdditionalData xmlns="http://ns.hr-xml.org/2007-04-15">
<CustomerReportingRequirements>
<PurchaseOrderNumber/>
<CostCenterCode>9523</CostCenterCode>
<ProjectCode/>
</CustomerReportingRequirements>
</StaffingAdditionalData>
</UserArea>
</Line>
<Line>
<LineNumber>2</LineNumber>
<ReasonCode/>
<Charges>
<TotalCharge>
<Total currency="EUR">863.55</Total>
</TotalCharge>
</Charges>
<Line>
<LineNumber>2A</LineNumber>
<Charges>
<TotalCharge>
<Total currency="EUR">823.55</Total>
</TotalCharge>
</Charges>
<Tax>
<PercentQuantity uom="BTW">21.00</PercentQuantity>
</Tax>
<Price>
<Amount currency="EUR">23.53</Amount>
<PerQuantity uom="regular">1</PerQuantity>
</Price>
<ItemQuantity uom="regular">35.00</ItemQuantity>
</Line>
<Line>
<LineNumber>2B</LineNumber>
<Charges>
<TotalCharge>
<Total currency="EUR">40.00</Total>
</TotalCharge>
</Charges>
<Tax>
<PercentQuantity uom="BTW">21.00</PercentQuantity>
</Tax>
<Price>
<Amount currency="EUR">8.00</Amount>
<PerQuantity uom="Travel expenses">1</PerQuantity>
</Price>
<ItemQuantity uom="Travel expenses">5.00</ItemQuantity>
</Line>
<UserArea>
<TimeCard xmlns="http://ns.hr-xml.org/2007-04-15">
<Id idOwner="StaffingCustomer">
<IdValue/>
</Id>
<ReportedResource>
<Person>
<Id idOwner="StaffingCustomer">
<IdValue>15ededed</IdValue>
</Id>
<PersonName>
<FormattedName>Mdededed</FormattedName>
<LegalName>M. Mideded</LegalName>
<GivenName>Mdededede</GivenName>
<PreferredGivenName>Mdedede</PreferredGivenName>
<FamilyName>Mideedede</FamilyName>
</PersonName>
</Person>
</ReportedResource>
<ReportedTime>
<PeriodStartDate>2019-05-20T00:00:00Z</PeriodStartDate>
<PeriodEndDate>2019-05-26T00:00:00Z</PeriodEndDate>
<PeriodWeekNum>21</PeriodWeekNum>
<TimeInterval type="regular" billable="true">
<StartDateTime>2019-05-20T00:00:00Z</StartDateTime>
<EndDateTime>2019-05-26T00:00:00Z</EndDateTime>
<Duration>35.00</Duration>
<RateOrAmount currency="EUR" type="Hourly" multiplier="100.00">23.53</RateOrAmount>
<AdditionalData>
<CustomerReportingRequirements>
<PurchaseOrderLineItem>0</PurchaseOrderLineItem>
</CustomerReportingRequirements>
</AdditionalData>
</TimeInterval>
<Allowance type="Travel expenses" billable="true">
<Id>
<IdValue name="allowance"/>
</Id>
<StartDate>2019-05-20T00:00:00Z</StartDate>
<EndDate>2019-05-26T00:00:00Z</EndDate>
<Amount currency="EUR">8.00</Amount>
<Quantity>5.00</Quantity>
<AdditionalData>
<CustomerReportingRequirements>
<PurchaseOrderLineItem>0</PurchaseOrderLineItem>
</CustomerReportingRequirements>
</AdditionalData>
</Allowance>
</ReportedTime>
<SubmitterInfo>
<SubmittedDateTime>2019-06-19</SubmittedDateTime>
</SubmitterInfo>
<AdditionalData>
<StaffingAdditionalData xmlns="http://ns.hr-xml.org/2007-04-15">
<CustomerReportingRequirements>
<PurchaseOrderNumber/>
<CostCenterCode>9523</CostCenterCode>
<ProjectCode/>
</CustomerReportingRequirements>
<ReferenceInformation>
<AssignmentId idOwner="StaffingCompany">
<IdValue>1510009552</IdValue>
</AssignmentId>
</ReferenceInformation>
</StaffingAdditionalData>
</AdditionalData>
</TimeCard>
<StaffingAdditionalData xmlns="http://ns.hr-xml.org/2007-04-15">
<CustomerReportingRequirements>
<PurchaseOrderNumber/>
<CostCenterCode>9523</CostCenterCode>
<ProjectCode/>
</CustomerReportingRequirements>
</StaffingAdditionalData>
</UserArea>
</Line>
</Invoice>

我知道XML编写得很差,但这就是我们得到的xml。我需要这样做。

预期结果应为2019W21,但实际结果现在为2019W。有人可以帮我解决我做错的事情吗?

PS:我删除了机密信息,这就是为什么您看到BLADIABLAD等的原因。

1 个答案:

答案 0 :(得分:0)

您遇到的主要问题是[ { "category_name": "Alcoholic Beverages", "category_id": 94, "parent_id": 0 }, { "category_name": "American", "category_id": 314, "parent_id": 0 }, { "category_name": "Australian Chicken", "category_id": 311, "parent_id": 304 }, { "category_name": "Austrian", "category_id": 304, "parent_id": 0 }, { "category_name": "Beer", "category_id": 286, "parent_id": 94 }, { "category_name": "Brazilian", "category_id": 298, "parent_id": 0 }, { "category_name": "Budweiser", "category_id": 287, "parent_id": 286 }, { "category_name": "Burgers", "category_id": 75, "parent_id": 0 }, { "category_name": "Burgers", "category_id": 315, "parent_id": 314 }, { "category_name": "Chicken Burger", "category_id": 309, "parent_id": 75 }, { "category_name": "Chicken Pasta Roll", "category_id": 281, "parent_id": 0 }, { "category_name": "Chicken Roll", "category_id": 308, "parent_id": 281 }, { "category_name": "Daily Specials", "category_id": 294, "parent_id": 0 }, { "category_name": "For the kid's", "category_id": 105, "parent_id": 0 }, { "category_name": "Great Bud Beer", "category_id": 288, "parent_id": 287 }, { "category_name": "Gujarati", "category_id": 357, "parent_id": 0 }, { "category_name": "Hawaiian", "category_id": 305, "parent_id": 0 }, { "category_name": "Indian", "category_id": 312, "parent_id": 0 }, { "category_name": "Indian Snacks", "category_id": 96, "parent_id": 81 }, { "category_name": "Italian ", "category_id": 297, "parent_id": 0 }, { "category_name": "Japanese", "category_id": 307, "parent_id": 0 }, { "category_name": "Kathiyavadi", "category_id": 358, "parent_id": 357 }, { "category_name": "Kathiyavadi", "category_id": 355, "parent_id": 353 }, { "category_name": "Local Favorites", "category_id": 280, "parent_id": 0 }, { "category_name": "Non Alcoholic Beverages", "category_id": 95, "parent_id": 0 }, { "category_name": "Other Beverages", "category_id": 93, "parent_id": 95 }, { "category_name": "Pasta", "category_id": 100, "parent_id": 0 }, { "category_name": "Pasta Roll", "category_id": 282, "parent_id": 281 }, { "category_name": "Penne Pasta", "category_id": 303, "parent_id": 100 }, { "category_name": "Pizza", "category_id": 101, "parent_id": 0 }, { "category_name": "Po'Boys", "category_id": 242, "parent_id": 0 }, { "category_name": "Sandwich", "category_id": 99, "parent_id": 0 }, { "category_name": "Side Dishes", "category_id": 81, "parent_id": 0 }, { "category_name": "Soft drinks", "category_id": 306, "parent_id": 94 }, { "category_name": "Soups", "category_id": 83, "parent_id": 0 }, { "category_name": "South American", "category_id": 359, "parent_id": 0 }, { "category_name": "South Indian", "category_id": 354, "parent_id": 0 }, { "category_name": "Speciality Drinks", "category_id": 92, "parent_id": 95 }, { "category_name": "Steak", "category_id": 299, "parent_id": 298 }, { "category_name": "Sundaes", "category_id": 86, "parent_id": 0 }, { "category_name": "Sushi", "category_id": 283, "parent_id": 242 }, { "category_name": "Sushi Bar", "category_id": 284, "parent_id": 0 }, { "category_name": "Value Meals", "category_id": 87, "parent_id": 0 }, { "category_name": "Vegitarian", "category_id": 313, "parent_id": 312 }, { "category_name": "Waffles", "category_id": 88, "parent_id": 0 }, { "category_name": "Wine", "category_id": 90, "parent_id": 94 } ] 及其所有后代,并且位于与Timecard不同的名称空间中

Invoice

因此,您需要在XSLT中声明该名称空间(此处将使用任何前缀。必须匹配的URI)

 <TimeCard xmlns="http://ns.hr-xml.org/2007-04-15">

然后您的xpath表达式变为:

xmlns:t="http://ns.hr-xml.org/2007-04-15"

关于获取最大周数,您需要用x:Line/x:UserArea/t:TimeCard/t:ReportedTime/t:PeriodWeekNum 替换xsl:if,然后可以将其按降序排序。

xsl:for-each

尝试使用完整的XSLT

<xsl:for-each select="x:Line/x:UserArea/t:TimeCard/t:ReportedTime/t:PeriodWeekNum">
  <xsl:sort data-type="number" order="descending" />
  <xsl:if test="position() = 1">
    <xsl:value-of select="." />
  </xsl:if>
</xsl:for-each>