0

我正在使用 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>
4

3 回答 3

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>
于 2012-12-06T03:28:54.803 回答
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()){
....
}

因此,您可以检查是否有必要以这种方式获取子节点值。这就是为什么我从控制台输出中获得所有空白空间的原因。

告诉我它是否有帮助;)

于 2012-12-06T04:13:53.307 回答
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();
于 2012-12-06T23:04:38.860 回答