使用XSL将动态XML转换为html

时间:2012-02-13 08:21:29

标签: xml xslt

我有一个动态获得的XML输入。我怎样才能为它编写样式表。我想以树形格式显示xml(使用ul& li)。我没有明确的想法实现它。 这是我的xml格式。请给我一个想法,从它开始。

<body>
<elements segmentid="0" segmentuid="_020" description="Beginning of Hierarchical Transaction" required="true" empty="false">
  <datas required="true" name="Hierarchical Structure Code" text="0022">0022</datas>
  <datas required="true" name="Transaction Set Purpose Code" usertext="13">13</datas>
  <datas required="false" name="Reference Identification">199</datas>
  <datas required="false" name="Date" format="CCYYMMDD" function="Date">20111219</datas>
  <datas required="false" name="Time" format="HHMMSS" function="Time">010720</datas>
  <datas required="false" name="Transaction Type Code" />
</elements>
<elements segmentid="1" segmentuid="2000A_010" description="Information Source Level" required="true" havechildren="True" empty="false">
  <elements segmentid="2" segmentuid="2100A_030" description="Information Source Name" required="true" empty="false" customid="1298">
    <datas required="true" name="Entity Identifier Code" usertext="PR">PR</datas>
    <datas required="true" name="Entity Type Qualifier" usertext="2">2</datas>
    <datas required="false" name="Name Last or Organization Name">UNITED</datas>
    <datas required="false" name="Name First" />
    <datas required="false" name="Name Middle" />
    <datas required="false" name="Name Prefix" />
    <datas required="false" name="Name Suffix" />
    <datas required="false" name="Identification Code Qualifier" usertext="PI">PI</datas>
    <datas required="false" name="Identification Code" />
    <datas required="false" name="Entity Relationship Code" />
    <datas required="false" name="Entity Identifier Code" />
  </elements>
</elements>
<elements segmentid="3" segmentuid="2000B_010" description="Information Receiver Level" required="true" havechildren="True" empty="false">
  <elements segmentid="4" segmentuid="2100B_030" description="Information Receiver Name" required="true" empty="false">
    <datas required="true" name="Entity Identifier Code" usertext="FA">FA</datas>
    <datas required="true" name="Entity Type Qualifier" />
    <datas required="false" name="Name Last or Organization Name">SWXRAY</datas>
    <datas required="false" name="Name First" />
    <datas required="false" name="Name Middle" />
    <datas required="false" name="Name Prefix" />
    <datas required="false" name="Name Suffix" usertext="" />
    <datas required="false" name="Identification Code Qualifier" usertext="FI">XX</datas>
    <datas required="false" name="Identification Code" usertext="760022799">1467524231</datas>
    <datas required="false" name="Entity Relationship Code" />
    <datas required="false" name="Entity Identifier Code" />
    <elements segmentid="5" segmentuid="2100B_040" description="Information Receiver Additional Identification" required="false" empty="true">
      <datas required="true" name="Reference Identification Qualifier" />
      <datas required="false" name="Reference Identification" />
      <datas required="false" name="Description" />
      <datas required="false" name="REFERENCE IDENTIFIER" />
    </elements>
    <elements segmentid="6" segmentuid="2100B_060" description="Information Receiver Address" required="false" empty="true">
      <datas required="true" name="Address Information" />
      <datas required="false" name="Address Information" />
    </elements>  
</elements>
<elements segmentid="10" segmentuid="2000C_010" description="Subscriber Level" required="true" havechildren="false" empty="false">
  <elements segmentid="11" segmentuid="2000C_020" description="Subscriber Trace Number" required="false" empty="true">
    <datas required="true" name="Trace Type Code" text="1">1</datas>
    <datas required="true" name="Reference Identification">199</datas>
    <datas required="false" name="Originating Company Identifier" text="9EMDEDI" usertext="9EMDEDI">9EMDEDI</datas>
    <datas required="false" name="Reference Identification" />
  </elements>
  <elements segmentid="12" segmentuid="2100C_030" description="Subscriber Name" required="true" empty="false">
    <datas required="true" name="Entity Identifier Code" text="IL">IL</datas>
    <datas required="true" name="Entity Type Qualifier" text="1" usertext="1">1</datas>
    <datas required="false" name="Name Last or Organization Name">dfgdfg</datas>
    <datas required="false" name="Name First">dfgdfgdf</datas>
    <datas required="false" name="Name Middle" />
    <datas required="false" name="Name Prefix" />
    <datas required="false" name="Name Suffix" />
    <datas required="false" name="Identification Code Qualifier" usertext="MI">MI</datas>
    <datas required="false" name="Identification Code">54645654</datas>
    <datas required="false" name="Entity Relationship Code" />
    <datas required="false" name="Entity Identifier Code" />
    <elements segmentid="13" segmentuid="2100C_040" description="Subscriber Additional Identification" required="false" empty="true">
      <datas required="true" name="Reference Identification Qualifier" />
      <datas required="false" name="Reference Identification" />
      <datas required="false" name="Description" />
      <datas required="false" name="REFERENCE IDENTIFIER" />
    </elements>
    <elements segmentid="14" segmentuid="2100C_060" description="Subscriber Address" required="false" empty="true">
      <datas required="true" name="Address Information" />
      <datas required="false" name="Address Information" />
    </elements>
    <elements segmentid="15" segmentuid="2100C_070" description="Subscriber City/State/ZIP Code" required="false" empty="true">
      <datas required="false" name="City Name" />
      <datas required="false" name="State or Province Code" />
      <datas required="false" name="Postal Code" />
      <datas required="false" name="Country Code" />
      <datas required="false" name="Location Qualifier" />
      <datas required="false" name="Location Identifier" />
    </elements>
    <elements segmentid="16" segmentuid="2100C_090" description="Provider Information" required="false" empty="true">
      <datas required="true" name="Provider Code" />
      <datas required="true" name="Reference Identification Qualifier" text="ZZ">ZZ</datas>
      <datas required="true" name="Reference Identification" />
      <datas required="false" name="State or Province Code" />
      <datas required="false" name="PROVIDER SPECIALTY INFORMATION" />
      <datas required="false" name="Provider Organization Code" />
    </elements>
    <elements segmentid="17" segmentuid="2100C_100" description="Subscriber Demographic Information" required="false" empty="false">
      <datas required="false" name="Date Time Period Format Qualifier" text="D8">D8</datas>
      <datas required="false" name="Date Time Period" format="CCYYMMDD" function="Date">19761010</datas>
      <datas required="false" name="Gender Code" usertext="" />
      <datas required="false" name="Marital Status Code" />
      <datas required="false" name="Race or Ethnicity Code" />
      <datas required="false" name="Citizenship Status Code" />
      <datas required="false" name="Country Code" />
      <datas required="false" name="Basis of Verification Code" />
      <datas required="false" name="Quantity" />
    </elements>
    <elements segmentid="18" segmentuid="2100C_110" description="Subscriber Relationship" required="false" empty="true">
      <datas required="true" name="Yes/No Condition or Response Code" text="Y">Y</datas>
      <datas required="true" name="Individual Relationship Code" text="18">18</datas>
      <datas required="false" name="Maintenance Type Code" text="001">001</datas>
      <datas required="false" name="Maintenance Reason Code" text="25">25</datas>
      <datas required="false" name="Benefit Status Code" />
      <datas required="false" name="Medicare Plan Code" />
      <datas required="false" name="Consolidated Omnibus Budget Reconciliation Act (COBRA) Qualifying" />
      <datas required="false" name="Employment Status Code" />
      <datas required="false" name="Student Status Code" />
      <datas required="false" name="Yes/No Condition or Response Code" />
      <datas required="false" name="Date Time Period Format Qualifier" />
      <datas required="false" name="Date Time Period" />
      <datas required="false" name="Confidentiality Code" />
      <datas required="false" name="City Name" />
      <datas required="false" name="State or Province Code" />
      <datas required="false" name="Country Code" />
      <datas required="false" name="Number" />
    </elements>
    <elements segmentid="19" segmentuid="2100C_120" description="Subscriber Date" required="false" empty="false">
      <datas required="true" name="Date/Time Qualifier" usertext="307">307</datas>
      <datas required="true" name="Date Time Period Format Qualifier" usertext="D8">D8</datas>
      <datas required="true" name="Date Time Period" format="CCYYMMDD" function="Date">20111219</datas>
    </elements>
    <elements segmentid="20" segmentuid="2110C_130" description="Subscriber Eligibility or Benefit Inquiry Information" required="false" empty="false" repeat="10">
      <datas required="false" name="Service Type Code">4</datas>
      <datas required="false" name="COMPOSITE MEDICAL PROCEDURE IDENTIFIER">
        <compositedatas required="true" name="Product/Service ID Qualifier" />
        <compositedatas required="true" name="Product/Service ID" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Description" />
      </datas>
      <datas required="false" name="Coverage Level Code" />
      <datas required="false" name="Insurance Type Code" />
      <elements segmentid="21" segmentuid="2110C_135" description="Subscriber Spend Down Amount" required="false" empty="true">
        <datas required="true" name="Amount Qualifier Code" text="R">R</datas>
        <datas required="true" name="Monetary Amount" />
        <datas required="false" name="Credit/Debit Flag Code" />
      </elements>
      <elements segmentid="22" segmentuid="2110C_170" description="Subscriber Eligibility or Benefit Additional Inquiry Information" required="false" empty="true">
        <datas required="false" name="Code List Qualifier Code" />
        <datas required="false" name="Industry Code" />
        <datas required="false" name="Code Category" />
        <datas required="false" name="Free-Form Message Text" />
        <datas required="false" name="Quantity" />
        <datas required="false" name="COMPOSITE UNIT OF MEASURE" />
        <datas required="false" name="Surface/Layer/Position Code" />
        <datas required="false" name="Surface/Layer/Position Code" />
        <datas required="false" name="Surface/Layer/Position Code" />
      </elements>
      <elements segmentid="23" segmentuid="2110C_190" description="Subscriber Additional Information" required="false" empty="true">
        <datas required="true" name="Reference Identification Qualifier" />
        <datas required="false" name="Reference Identification" />
        <datas required="false" name="Description" />
        <datas required="false" name="REFERENCE IDENTIFIER" />
      </elements>
      <elements segmentid="24" segmentuid="2110C_200" description="Subscriber Eligibility/Benefit Date" required="false" empty="true">
        <datas required="true" name="Date/Time Qualifier" />
        <datas required="true" name="Date Time Period Format Qualifier" />
        <datas required="true" name="Date Time Period" format="CCYYMMDD(or)CCYYMMDD-CCYYMMDD" />
      </elements>
    </elements>
    <elements segmentid="20" segmentuid="2110C_130" description="Subscriber Eligibility or Benefit Inquiry Information" required="false" empty="false" repeat="10">
      <datas required="false" name="Service Type Code">30</datas>
      <datas required="false" name="COMPOSITE MEDICAL PROCEDURE IDENTIFIER">
        <compositedatas required="true" name="Product/Service ID Qualifier" />
        <compositedatas required="true" name="Product/Service ID" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Procedure Modifier" />
        <compositedatas required="false" name="Description" />
      </datas>
      <datas required="false" name="Coverage Level Code" />
      <datas required="false" name="Insurance Type Code" />
      <elements segmentid="21" segmentuid="2110C_135" description="Subscriber Spend Down Amount" required="false" empty="true">
        <datas required="true" name="Amount Qualifier Code" text="R">R</datas>
        <datas required="true" name="Monetary Amount" />
        <datas required="false" name="Credit/Debit Flag Code" />
      </elements>
      <elements segmentid="22" segmentuid="2110C_170" description="Subscriber Eligibility or Benefit Additional Inquiry Information" required="false" empty="true">
        <datas required="false" name="Code List Qualifier Code" />
        <datas required="false" name="Industry Code" />
        <datas required="false" name="Code Category" />
        <datas required="false" name="Free-Form Message Text" />
        <datas required="false" name="Quantity" />
        <datas required="false" name="COMPOSITE UNIT OF MEASURE" />
        <datas required="false" name="Surface/Layer/Position Code" />
        <datas required="false" name="Surface/Layer/Position Code" />
        <datas required="false" name="Surface/Layer/Position Code" />
      </elements>
      <elements segmentid="23" segmentuid="2110C_190" description="Subscriber Additional Information" required="false" empty="true">
        <datas required="true" name="Reference Identification Qualifier" />
        <datas required="false" name="Reference Identification" />
        <datas required="false" name="Description" />
        <datas required="false" name="REFERENCE IDENTIFIER" />
      </elements>
      <elements segmentid="24" segmentuid="2110C_200" description="Subscriber Eligibility/Benefit Date" required="false" empty="true">
        <datas required="true" name="Date/Time Qualifier" />
        <datas required="true" name="Date Time Period Format Qualifier" />
        <datas required="true" name="Date Time Period" format="CCYYMMDD(or)CCYYMMDD-CCYYMMDD" />
      </elements>
    </elements>
  </elements>
</elements>
<elements segmentid="25" segmentuid="2000D_010" description="Dependent Level" required="false" havechildren="False" empty="true">
  <elements segmentid="26" segmentuid="2000D_020" description="Dependent Trace Number" required="false" empty="true">
    <datas required="true" name="Trace Type Code" text="1">1</datas>
    <datas required="true" name="Reference Identification">199</datas>
    <datas required="false" name="Originating Company Identifier" text="9EMDEDI" usertext="9EMDEDI">9EMDEDI</datas>
    <datas required="false" name="Reference Identification" />
  </elements>
  <elements segmentid="27" segmentuid="2100D_030" description="Dependent Name" required="true" empty="false">
    <datas required="true" name="Entity Identifier Code" text="03">03</datas>
    <datas required="true" name="Entity Type Qualifier" text="1">1</datas>
    <datas required="false" name="Name Last or Organization Name">dfgdfg</datas>
    <datas required="false" name="Name First">dfgdfgdf</datas>
    <datas required="false" name="Name Middle" />
    <datas required="false" name="Name Prefix" />
    <datas required="false" name="Name Suffix" />
    <datas required="false" name="Identification Code Qualifier" />
    <datas required="false" name="Identification Code" />
    <datas required="false" name="Entity Relationship Code" />
    <datas required="false" name="Entity Identifier Code" />
    <elements segmentid="28" segmentuid="2100D_040" description="Dependent Additional Identification" required="false" empty="true">
      <datas required="true" name="Reference Identification Qualifier" />
      <datas required="false" name="Reference Identification" />
      <datas required="false" name="Description" />
      <datas required="false" name="REFERENCE IDENTIFIER" />
    </elements>
    <elements segmentid="29" segmentuid="2100D_060" description="Dependent Address" required="false" empty="true">
      <datas required="true" name="Address Information" />
      <datas required="false" name="Address Information" />
    </elements>
 </elements>

  

我的渴望应该是

<ul>1.Beginning of Hierarchical Transaction
           <li>Hierarchical Structure Code-0022</li>
           <li>Transaction Set Purpose Code-13</li>
           <li>Reference Identification-199</li>
           <li>Date-20111219</li>
           <li>Time-010720</li>
</ul>    
<ul>2.Information Source Level
<ul>a.Information Source Name
           <li>Entity Identifier Code-PR</li>
           <li>Entity Type Qualifier-2</li>
           <li>Name Last or Organization Name-UNITED</li>
           <li>Name First-""</li>
           <li>Name Middle-""</li>

喜欢这个。

2 个答案:

答案 0 :(得分:1)

正如Filiburt所说,你想要如何组织你的输出并不是很清楚,但同时你声明你只需要一些帮助就可以让你继续前进,所以这里有一个解决方案草案:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
    <ul>
    <xsl:apply-templates/>
   </ul>
    </xsl:template>

    <xsl:template match="/body/elements[@segmentid='0']">
        <li><xsl:value-of select="/body/elements[@segmentid=0]/datas[@name='Hierarchical Structure Code']/@name"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="/body/elements[@segmentid=0]/datas[@name='Hierarchical Structure Code']"/></li>
         <li>
        <xsl:value-of select="/body/elements[@segmentid=0]/datas[@name='Transaction Set Purpose Code']/@name"/>
         <xsl:text>-</xsl:text>
        <xsl:value-of select="/body/elements[@segmentid=0]/datas[@name='Transaction Set Purpose Code']"/></li>
    </xsl:template>

    <xsl:template match="/body/elements/elements">
    <ul>
        <li>
        <xsl:value-of select="./datas[@name='Entity Identifier Code']/@name"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="./datas[@name='Entity Identifier Code']"/>
        </li>
        <li>
        <xsl:value-of select="./datas[@name='Entity Type Qualifier']/@name"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="./datas[@name='Entity Type Qualifier']"/>
        </li>
        <li>
        <xsl:value-of select="./datas[@name='Name Last or Organization Name']/@name"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="./datas[@name='Name Last or Organization Name']"/>
        </li>
   </ul>
    </xsl:template>

</xsl:transform>

这将为您提供以下输出:

<ul xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <li>Hierarchical Structure Code-0022</li>
  <li>Transaction Set Purpose Code-13</li>
  <ul>
    <li>Entity Identifier Code Entity Identifier Code-PR</li>
    <li>Entity Type Qualifier-2</li>
    <li>Name Last or Organization Name-UNITED</li>
  </ul>
  <ul>
    <li>Entity Identifier Code Entity Identifier Code-FA</li>
    <li>Entity Type Qualifier-</li>
    <li>Name Last or Organization Name-SWXRAY</li>
  </ul>
</ul>

答案 1 :(得分:0)

这是一个递归变体。它使用<ol>代替<ul>进行编号。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*|@*|text()"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Elements</title>
                <style type="text/css">
                    <xsl:text>ol { list-style-type: decimal; }</xsl:text>
                    <xsl:text>ol ol { list-style-type: lower-alpha; }</xsl:text>
                    <xsl:text>ol ol ol { list-style-type: upper-roman; }</xsl:text>
                </style>
            </head>
            <body>
                <ol>
                    <xsl:apply-templates select="/body/*"/>
                </ol>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="elements">
        <li>
            <xsl:value-of select="@description"/>
            <xsl:if test="datas">
                <ul>
                    <xsl:apply-templates select="datas"/>
                </ul>
            </xsl:if>
            <xsl:if test="elements">
                <ol>
                    <xsl:apply-templates select="elements"/>
                </ol>
            </xsl:if>
        </li>
    </xsl:template>

    <xsl:template match="datas">
        <li>
            <xsl:value-of select="@name"/>
            <xsl:text>-</xsl:text>
            <xsl:value-of select="text()"/>
        </li>
    </xsl:template>

    <xsl:template match="datas[compositedatas]">
        <li>
            <xsl:value-of select="@name"/>
            <ul>
                <xsl:apply-templates select="compositedatas"/>
            </ul>
        </li>
    </xsl:template>

    <xsl:template match="compositedatas">
        <li>
            <xsl:value-of select="@name"/>
            <xsl:text>-</xsl:text>
            <xsl:value-of select="text()"/>
        </li>
    </xsl:template>

</xsl:stylesheet>

输出:

      
  1. 分层事务的开始
            
    • 分层结构代码-0022
    •       
    • 交易集目的代码-13
    •       
    • 参考识别-199
    •       
    • 日期-20111219
    •       
    • 时间-010720
    •       
    • 交易类型代码 -
    •     
      
  2.   
  3. 信息来源等级
            
    1. 信息来源名称
                  
      • 实体标识符代码-PR
      •           
      • 实体类型限定符-2
      •           
      • 姓名姓氏或组织名称-UNITED
      •           
      • 名字第一 -
      •           
      • 名称中间 -
      •           
      • 名称前缀 -
      •           
      • 名称后缀 -
      •           
      • 识别码限定符-PI
      •           
      • 识别码 -
      •           
      • 实体关系代码 -
      •           
      • 实体标识符代码 -
      •         
            
    2.           
    3. 信息接收器级别
              
      1. 信息接收者姓名
                    
        • 实体标识符代码-FA
        •           
        • 实体类型限定符 -
        •           
        • 姓名姓氏或组织名称-SWXRAY
        •           
        • 名字第一 -
        •           
        • 名称中间 -
        •           
        • 名称前缀 -
        •           
        • 名称后缀 -
        •           
        • 识别码限定符-XX
        •           
        • 识别代码-1467524231
        •           
        • 实体关系代码 -
        •           
        • 实体标识符代码 -
        •         
                
                    
        1. 信息接收者附加识别
                          
          • 参考识别限定符 -
          •               
          • 参考标识 -
          •               
          • 描述 -
          •               
          • 参考标识符 -
          •             
                    
        2.           
        3. 信息接收者地址
                          
          • 地址信息 -
          •               
          • 地址信息 -
          •             
                    
        4.                  ...               

          内部编号列表具有不同的格式,但无法在SO

          上表示

          HTML:

          <html>
            <head>
              <title>Elements</title>
              <style type="text/css">ol { list-style-type: decimal; }ol ol { list-style-type: lower-alpha; }ol ol ol { list-style-type: upper-roman; }</style>
            </head>
            <body>
              <ol>
                <li>Beginning of Hierarchical Transaction<ul>
                    <li>Hierarchical Structure Code-0022</li>
                    <li>Transaction Set Purpose Code-13</li>
                    <li>Reference Identification-199</li>
                    <li>Date-20111219</li>
                    <li>Time-010720</li>
                    <li>Transaction Type Code-</li>
                  </ul>
                </li>
                <li>Information Source Level<ol>
                    <li>Information Source Name<ul>
                        <li>Entity Identifier Code-PR</li>
                        <li>Entity Type Qualifier-2</li>
                        <li>Name Last or Organization Name-UNITED</li>
                        <li>Name First-</li>
                        <li>Name Middle-</li>
                        <li>Name Prefix-</li>
                        <li>Name Suffix-</li>
                        <li>Identification Code Qualifier-PI</li>
                        <li>Identification Code-</li>
                        <li>Entity Relationship Code-</li>
                        <li>Entity Identifier Code-</li>
                      </ul>
                    </li>
                  </ol>
                </li>
                <li>Information Receiver Level<ol>
                    <li>Information Receiver Name<ul>
                        <li>Entity Identifier Code-FA</li>
                        <li>Entity Type Qualifier-</li>
                        <li>Name Last or Organization Name-SWXRAY</li>
                        <li>Name First-</li>
                        <li>Name Middle-</li>
                        <li>Name Prefix-</li>
                        <li>Name Suffix-</li>
                        <li>Identification Code Qualifier-XX</li>
                        <li>Identification Code-1467524231</li>
                        <li>Entity Relationship Code-</li>
                        <li>Entity Identifier Code-</li>
                      </ul>
                      <ol>
                        <li>Information Receiver Additional Identification<ul>
                            <li>Reference Identification Qualifier-</li>
                            <li>Reference Identification-</li>
                            <li>Description-</li>
                            <li>REFERENCE IDENTIFIER-</li>
                          </ul>
                        </li>
                        <li>Information Receiver Address<ul>
                            <li>Address Information-</li>
                            <li>Address Information-</li>
                          </ul>
                        </li>
                      </ol>
                      ...
                    </li>
                  </ol>
                </li>
              </ol>
            </body>
          </html>