2012-03-07 3 views
2

Я пытаюсь взять XML-канал из стороннего источника и сгладить его.Сгладить сторонний XML-канал?

В настоящее время подача XML напоминает это:

<properties> 
    <property> 
     <idnumber></idnumber> 
     <location> 
      <region></region> 
      <street-address></street-address> 
      <city-name></city-name> 
      <state-code></state-code> 
      <zipcode></zipcode> 
      <latitude></latitude> 
      <longitude></longitude> 
     </location> 
     <details> 
      <name></name> 
      <status></status> 
      <price></price> 
      <bedrooms></num-bedrooms> 
      <bathrooms></bathrooms> 
      <lot-size></lot-size> 
      <square-feet></square-feet> 
      <property-type></property-type> 
      <attributes></attributes> 
      <description></description> 
     </details> 
     <pictures> 
      <picture> 
       <picture-url></picture-url> 
      </picture> 
    </property> 
</properties> 

Однако, мне действительно нужно выравнивать XML для этого:

<properties> 
    <property> 
     <idnumber></idnumber> 
     <region></region> 
     <street-address></street-address> 
     <city-name></city-name> 
     <state-code></state-code> 
     <zipcode></zipcode> 
     <latitude></latitude> 
     <longitude></longitude> 
     <name></name> 
     <status></status> 
     <price></price> 
     <bedrooms></num-bedrooms> 
     <bathrooms></bathrooms> 
     <lot-size></lot-size> 
     <square-feet></square-feet> 
     <property-type></property-type> 
     <attributes></attributes> 
     <description></description> 
     <picture> 
       <picture-url></picture-url> 
     </picture> 
    </property> 
</properties> 

Я читал вверх на XLST таблицы стилей, чтобы попытаться это , однако я просто трачу свое время, поскольку исходный фид размещен на третьей стороне, в которой я не могу редактировать?

ответ

1

Это сделает то, что вы хотите. Шаблон идентичности копирует все в выходной файл, в то время как другие три шаблона заставляют теги второго уровня быть опущены при копировании их содержимого.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

<xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="property/location"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="property/details"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="property/pictures"> 
    <xsl:apply-templates/> 
</xsl:template> 

</xsl:stylesheet> 
1

Это преобразование:

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

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/*/*/*[*]"> 
    <xsl:apply-templates/> 
</xsl:template> 
</xsl:stylesheet> 

при нанесении на прилагаемом документе XML (скорректированный быть сделаны хорошо сформированные):

<properties> 
    <property> 
     <idnumber></idnumber> 
     <location> 
      <region></region> 
      <street-address></street-address> 
      <city-name></city-name> 
      <state-code></state-code> 
      <zipcode></zipcode> 
      <latitude></latitude> 
      <longitude></longitude> 
     </location> 
     <details> 
      <name></name> 
      <status></status> 
      <price></price> 
      <bedrooms></bedrooms> 
      <bathrooms></bathrooms> 
      <lot-size></lot-size> 
      <square-feet></square-feet> 
      <property-type></property-type> 
      <attributes></attributes> 
      <description></description> 
     </details> 
     <pictures> 
      <picture> 
       <picture-url></picture-url> 
      </picture> 
     </pictures> 
    </property> 
</properties> 

производит желаемое, правильный вывод :

<properties> 
    <property> 
     <idnumber/> 
     <region/> 
     <street-address/> 
     <city-name/> 
     <state-code/> 
     <zipcode/> 
     <latitude/> 
     <longitude/> 
     <name/> 
     <status/> 
     <price/> 
     <bedrooms/> 
     <bathrooms/> 
     <lot-size/> 
     <square-feet/> 
     <property-type/> 
     <attributes/> 
     <description/> 
     <picture> 
     <picture-url/> 
     </picture> 
    </property> 
</properties> 

Пояснение: Надлежащее использование и переопределение the identity rule.

+0

'' отсутствует –

+0

@JimGarrison: Хорошее наблюдение, исправлено. :) –

Смежные вопросы