2012-12-06 110 views
0

我正在使用saxon 9he,eclipse,tomcat 7.我將會解釋代碼。當我運行轉換時,除了InsurerId,我獲得了每個想要的值。如果我將xml更改(調試)到InsurerId2和xsl以獲取InsurerId2的值。我有兩個字段給了我這個麻煩,我曾經有一段時間能夠獲得這個時間,我已經添加了改爲xsl 2.0,從saxon 6改爲添加了當前日期函數。這是非常困惑,請幫助。Xsl沒有找到正確的XPath值

XML

<PersDriver id="001"> 
       <ItemIdInfo> 
        <InsurerId>001</InsurerId> 
       </ItemIdInfo> 
       <GeneralPartyInfo> 
        <NameInfo> 
          <PersonName> 
           <Surname>test</Surname> 
           <GivenName>test</GivenName> 
           <OtherGivenName> 
           </OtherGivenName> 
           <TitlePrefix> 
           </TitlePrefix> 
           <NameSuffix> 
           </NameSuffix> 
          </PersonName> 
          <TaxIdentity> 
           <TaxIdTypeCd>SSN</TaxIdTypeCd> 
           <TaxId> 999999999999</TaxId> 
          </TaxIdentity> 
        </NameInfo> 
       </GeneralPartyInfo> 
       <DriverInfo> 
        <PersonInfo> 
          <GenderCd>M</GenderCd> 
          <BirthDt>1979-08-23</BirthDt> 
          <MaritalStatusCd> 
          </MaritalStatusCd> 
        </PersonInfo> 
        <DriversLicense> 
          <LicensedDt>2007-08-23</LicensedDt> 
          <DriversLicenseNumber>99999999</DriversLicenseNumber> 
          <StateProvCd>TN</StateProvCd> 
        </DriversLicense> 
       </DriverInfo> 
       <PersDriverInfo> 
        <DefensiveDriverDt /> 
        <DriverRelationshipToApplicantCd> 
        </DriverRelationshipToApplicantCd> 
        <DriverTrainingInd /> 
        <GoodDriverInd /> 
        <GoodStudentCd /> 
       </PersDriverInfo> 
      </PersDriver> 

XSL

<xsl:for-each select="$RsXml/InsuranceSvcRs/com.csc_PolicyOrderCurrentCarrierInqRs/PersDriver"> 



     <driver type_code="DP" id="0"> 

     <prefix> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/TitlePrefix"/> 
     </prefix> 
     <firstname> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/GivenName"/> 
     </firstname> 
     <midname/> 
     <lastname> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/Surname"/>   
     </lastname> 
     <suffix> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/NameSuffix"/> 
     </suffix> 
     <sex> 
      <xsl:value-of select="DriverInfo/PersonInfo/GenderCd"/> 
     </sex> 
     <dateofbirth> 
      <xsl:value-of select="DriverInfo/PersonInfo/BirthDt"/> 
     </dateofbirth> 
     <ssn> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/TaxIdentity/TaxId"/>    
     </ssn> 
     <maritalstatus> 
      <xsl:value-of select="DriverInfo/PersonInfo/MaritalStatusCd"/> 
     </maritalstatus> 
     <occupation/> 
     <homephone/> 
     <workphone/> 
     <cellphone/> 
     <homeemail/> 
     <workemail/> 
     <licensestate> 
      <xsl:value-of select="DriverInfo/DriversLicense/StateProvCd"/> 
     </licensestate> 
     <licensenumber> 
      <xsl:value-of select="DriverInfo/DriversLicense/DriversLicenseNumber"/> 
     </licensenumber> 
     <xsl:message> 
     <TEST> 
     <xsl:value-of select="ItemIdInfo/InsurerId"/> 
     </TEST> 

     </xsl:message> 
     <xsl:choose>   
      <xsl:when test="@id=001">    
       <namedinsured>true</namedinsured>  
      </xsl:when> 
      <xsl:otherwise> 
       <namedinsured>false</namedinsured>    
      </xsl:otherwise> 
     </xsl:choose> 

     <namedinsured2></namedinsured2> 
     <driver></driver> 
     <livewithparents/> 
     <vehicledriven/> 
     <driveworkschool/> 
     <keptatcollege/> 
     <beensuspended/> 
     <business></business> 
     <beenarrested/> 
     <client_id></client_id> 
     <violations/> 
     <accidents/> 
    </driver> 
     </xsl:for-each> 

</xsl:copy> 
</xsl:template> 

回答

0

我不能重現該問題

這種變換(所提供的一個,但具有校正語法和一個匹配 - 內模板):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="PersDriver"> 
     <driver type_code="DP" id="0"> 

     <prefix> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/TitlePrefix"/> 
     </prefix> 
     <firstname> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/GivenName"/> 
     </firstname> 
     <midname/> 
     <lastname> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/Surname"/> 
     </lastname> 
     <suffix> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/PersonName/NameSuffix"/> 
     </suffix> 
     <sex> 
      <xsl:value-of select="DriverInfo/PersonInfo/GenderCd"/> 
     </sex> 
     <dateofbirth> 
      <xsl:value-of select="DriverInfo/PersonInfo/BirthDt"/> 
     </dateofbirth> 
     <ssn> 
      <xsl:value-of select="GeneralPartyInfo/NameInfo/TaxIdentity/TaxId"/> 
     </ssn> 
     <maritalstatus> 
      <xsl:value-of select="DriverInfo/PersonInfo/MaritalStatusCd"/> 
     </maritalstatus> 
     <occupation/> 
     <homephone/> 
     <workphone/> 
     <cellphone/> 
     <homeemail/> 
     <workemail/> 
     <licensestate> 
      <xsl:value-of select="DriverInfo/DriversLicense/StateProvCd"/> 
     </licensestate> 
     <licensenumber> 
      <xsl:value-of select="DriverInfo/DriversLicense/DriversLicenseNumber"/> 
     </licensenumber> 
     <xsl:message> 
     <TEST> 
     <xsl:value-of select="ItemIdInfo/InsurerId"/> 
     </TEST> 

     </xsl:message> 
     <xsl:choose> 
      <xsl:when test="@id=001"> 
       <namedinsured>true</namedinsured> 
      </xsl:when> 
      <xsl:otherwise> 
       <namedinsured>false</namedinsured> 
      </xsl:otherwise> 
     </xsl:choose> 

     <namedinsured2></namedinsured2> 
     <driver></driver> 
     <livewithparents/> 
     <vehicledriven/> 
     <driveworkschool/> 
     <keptatcollege/> 
     <beensuspended/> 
     <business></business> 
     <beenarrested/> 
     <client_id></client_id> 
     <violations/> 
     <accidents/> 
    </driver> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<PersDriver id="001"> 
    <ItemIdInfo> 
     <InsurerId>001</InsurerId> 
    </ItemIdInfo> 
    <GeneralPartyInfo> 
     <NameInfo> 
      <PersonName> 
       <Surname>DALESANDRO</Surname> 
       <GivenName>MAKIEH</GivenName> 
       <OtherGivenName> 
       </OtherGivenName> 
       <TitlePrefix> 
       </TitlePrefix> 
       <NameSuffix> 
       </NameSuffix> 
      </PersonName> 
      <TaxIdentity> 
       <TaxIdTypeCd>SSN</TaxIdTypeCd> 
       <TaxId>666-18-6774</TaxId> 
      </TaxIdentity> 
     </NameInfo> 
    </GeneralPartyInfo> 
    <DriverInfo> 
     <PersonInfo> 
      <GenderCd>M</GenderCd> 
      <BirthDt>1979-08-23</BirthDt> 
      <MaritalStatusCd> 
      </MaritalStatusCd> 
     </PersonInfo> 
     <DriversLicense> 
      <LicensedDt>2007-08-23</LicensedDt> 
      <DriversLicenseNumber>882827206</DriversLicenseNumber> 
      <StateProvCd>TN</StateProvCd> 
     </DriversLicense> 
    </DriverInfo> 
    <PersDriverInfo> 
     <DefensiveDriverDt /> 
     <DriverRelationshipToApplicantCd> 
     </DriverRelationshipToApplicantCd> 
     <DriverTrainingInd /> 
     <GoodDriverInd /> 
     <GoodStudentCd /> 
    </PersDriverInfo> 
</PersDriver> 

正好產生(含<namedinsured>true</namedinsured>)預期輸出:

<driver type_code="DP" id="0"> 
    <prefix> 
       </prefix> 
    <firstname>MAKIEH</firstname> 
    <midname/> 
    <lastname>DALESANDRO</lastname> 
    <suffix> 
       </suffix> 
    <sex>M</sex> 
    <dateofbirth>1979-08-23</dateofbirth> 
    <ssn>666-18-6774</ssn> 
    <maritalstatus> 
      </maritalstatus> 
    <occupation/> 
    <homephone/> 
    <workphone/> 
    <cellphone/> 
    <homeemail/> 
    <workemail/> 
    <licensestate>TN</licensestate> 
    <licensenumber>882827206</licensenumber> 
    <namedinsured>true</namedinsured> 
    <namedinsured2/> 
    <driver/> 
    <livewithparents/> 
    <vehicledriven/> 
    <driveworkschool/> 
    <keptatcollege/> 
    <beensuspended/> 
    <business/> 
    <beenarrested/> 
    <client_id/> 
    <violations/> 
    <accidents/> 
</driver> 
+0

此XML到XSL通過了PARM。你認爲ide或者parm可能會導致這個古怪的問題嗎?謝謝 –

+0

@JustinCox,如果傳入的是XmlDocument對象,則結果必須相同。但是,如果您將XML作爲文本傳遞,那麼這是一個錯誤 - 不應該這樣做。 –

+0

這是一個DOM,Michael告訴我要用XdmNode來改變這個變化。此外,這個應用程序緩存了xsl。我不明白爲什麼當我改變它的元素名稱的時候,看起來像是一個緩存的問題。 –

0

如果你想避免InsurerId,你可以試試這下面。 test.xml包含你的xml。

try { 

      Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("D:\\Loic_Workspace\\Test2\\res\\test.xml")); 

      XPath xpath = XPathFactory.newInstance().newXPath(); 
      NodeList ndList = (NodeList) xpath.evaluate("PersDriver/*[not(name()='ItemIdInfo')]", doc, XPathConstants.NODESET); 


      for(int i=0;i<ndList.getLength();i++){ 
       System.out.println(ndList.item(i).getNodeName()+" "+ndList.item(i).getTextContent()); 
      } 


     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ParserConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (XPathExpressionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

這將在將控制檯輸出:

GeneralPartyInfo 


DALESANDRO 
MAKIEH 








SSN 
666-18-6774 



DriverInfo 

M 
1979-08-23 




2007-08-23 
882827206 
TN 


PersDriverInfo 

你可以去檢查,如果一個節點包含的子節點做這個以下,例如:

if(ndList.item(i).hasChildNodes()){ 
.... 
} 

所以,你可以檢查如果有必要以這種方式獲取childen節點值。這就是爲什麼我從控制檯輸出中獲得所有空白空間的原因。

告訴我,如果它的幫助;)

0

我正在建的PARM XML與撒克遜下載券刪除它,它現在的作品???

之前

DocumentBuilderFactory dfactory = 
     DocumentBuilderFactory.newInstance("net.sf.saxon.dom.DocumentBuilderFactoryImpl",null); 
DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 
org.w3c.dom.Document document = docBuilder.parse(new InputSource(new StringReader(xmlSource))); 
Transformer transformer = xsl.newTransformer(); 
transformer.setParameter("RsXml", document.getDocumentElement()); 

改回

DocumentBuilderFactory dfactory =DocumentBuilderFactory.newInstance();