Xsl没有找到正确的XPath值

时间:2012-12-06 00:08:58

标签: java xml xslt xslt-2.0 saxon

我正在使用saxon 9he,eclipse,tomcat 7.我将解释代码。当我运行转换时,除了InsurerId之外,我得到了我想要的所有值。如果我将(在调试中)xml更改为InsurerId2和xsl以获取InsurerId2的值,则它可以正常工作。我有两个字段给我这个麻烦,我曾经能够在那段时间之间得到这个,我已经添加了更改为xsl 2.0,从saxon 6更改,添加了当前日期功能。请帮忙,这非常令人困惑。

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>

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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节点值。这就是我从控制台输出中获得所有空白的原因。

告诉我它是否有帮助;)

答案 2 :(得分:0)

我正在用saxon Impl构建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();