Преобразование динамического XML в HTML с использованием XSL - PullRequest
0 голосов
/ 13 февраля 2012

У меня есть ввод 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 ]

1 голос
/ 13 февраля 2012

Как сказал Филибурт, не совсем понятно, как вы хотите организовать выходной сигнал, но в то же время вы заявляете, что вам нужна только некоторая помощь, чтобы помочь вам в этом, поэтому вот вариант решения для работы:

<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>
0 голосов
/ 14 февраля 2012

Вот рекурсивный вариант. Для нумерации используется <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
    • Time-010720
    • Код типа транзакции -
  2. Уровень источника информации
    1. Название источника информации
      • Идентификатор объекта Код-PR
      • Тип сущности Qualifier-2
      • Фамилия или название организации - UNITED
      • Имя Имя-
      • Имя Middle-
      • Префикс имени-
      • Имя Суффикс-
      • Идентификационный код классификатора-PI
      • Идентификационный код-
      • Код Сущности -
      • Идентификационный код организации -
  3. Уровень получения информации
    1. Имя получателя информации
      • Идентификационный код объекта-FA
      • Классификатор типа сущности-
      • Имя Фамилия или Название организации-SWXRAY
      • Имя Имя-
      • Имя Middle-
      • Префикс имени-
      • Имя Суффикс-
      • Идентификационный код классификатора-XX
      • Идентификационный код-1467524231
      • Код отношения сущностей-
      • Код идентификации объекта -
      1. Дополнительная идентификация получателя информации
        • Ссылочный идентификационный классификатор-
        • Идентификационный номер-
        • Description-
        • ИДЕНТИФИКАТОР ССЫЛКИ-
      2. Адрес получателя информации
        • Информация об адресе-
        • Информация об адресе-
      ...

Внутренние нумерованные списки имеют разные форматы, но это не может быть представлено в 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...