XSLT能够做到这一点吗?

时间:2011-05-09 16:34:22

标签: xml xslt

它与我之前提出的问题不同(使用^^),因为这是一个不同的问题,它更像一些噪音和不需要的部分。 无论如何,我有一个文本文件,其内容是:

#HEADER#
Version : 3
EOF : '^'
EOR : '~'
Property Count : 67
Generated Date : 10-March-2011 17:39

#DEFINITION#
AGENT_REF^ADDRESS_1^ADDRESS_2^ADDRESS_3^ADDRESS_4^TOWN^POSTCODE1^POSTCODE2^FEATURE1^FEATURE2^FEATURE3^FEATURE4^FEATURE5^FEATURE6^FEATURE7^FEATURE8^FEATURE9^FEATURE10^SUMMARY^DESCRIPTION^BRANCH_ID^STATUS_ID^BEDROOMS^PRICE^PRICE_QUALIFIER^PROP_SUB_ID^UPDATE_DATE^CREATE_DATE^DISPLAY_ADDRESS^PUBLISHED_FLAG^LET_DATE_AVAILABLE^LET_BOND^LET_TYPE_ID^LET_FURN_ID^LET_RENT_FREQUENCY^TENURE_TYPE_ID^TRANS_TYPE_ID^NEW_HOME_FLAG^MEDIA_IMAGE_00^MEDIA_IMAGE_TEXT_00^MEDIA_IMAGE_01^MEDIA_IMAGE_TEXT_01^MEDIA_IMAGE_02^MEDIA_IMAGE_TEXT_02^MEDIA_IMAGE_03^MEDIA_IMAGE_TEXT_03^MEDIA_IMAGE_04^MEDIA_IMAGE_TEXT_04^MEDIA_IMAGE_05^MEDIA_IMAGE_TEXT_05^MEDIA_IMAGE_06^MEDIA_IMAGE_TEXT_06^MEDIA_IMAGE_07^MEDIA_IMAGE_TEXT_07^MEDIA_IMAGE_08^MEDIA_IMAGE_TEXT_08^MEDIA_IMAGE_09^MEDIA_IMAGE_TEXT_09^MEDIA_IMAGE_10^MEDIA_IMAGE_TEXT_10^MEDIA_IMAGE_11^MEDIA_IMAGE_TEXT_11^MEDIA_IMAGE_12^MEDIA_IMAGE_TEXT_12^MEDIA_IMAGE_13^MEDIA_IMAGE_TEXT_13^MEDIA_IMAGE_14^MEDIA_IMAGE_TEXT_14^MEDIA_IMAG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A_VIRTUAL_TOUR_TEXT_00^~

#DATA#
12345_ALZ57065V^^Spacious 3 bedroom villa with pool in Vale da Telha^^^^8670-156^^^^^^^^^^^^3 bedrooms villa with pool located in picturesque urbanization of Vale da Telha, on a private corner plot. The villa benefits from all day sun shine.   It has a mature settled garden with low maintenance and offers privacy for the villa. The villa comprises of living/dining room with wooden ceiling making the villa very welcoming. It has an open fireplace. The living room leads to the rustic yet modern kitchen, also on to the swimming pool area with BBQ and garden. Three bedrooms all with fitted wardrobes, (1 en suite) and wooden ceilings. All have good sun exposure, light and bright. The...^3 bedrooms villa with pool located in picturesque urbanization of Vale da Telha, on a private corner plot. The villa benefits from all day sun shine.   It has a mature settled garden with low maintenance and offers privacy for the villa. The villa comprises of living/dining room with wooden ceiling making the villa very welcoming. It has an open fireplace. The living room leads to the rustic yet modern kitchen, also on to the swimming pool area with BBQ and garden. Three bedrooms all with fitted wardrobes, (1 en suite) and wooden ceilings. All have good sun exposure, light and bright. The detached garage has ample storage space in form of a mezzanine, plumbing for washing machine. Irrigation system. A/C that gives hot and cold air and dehumifies. Good investment for those looking for a permanent home.<br/><br/><br/>^12345^0^3^277000^0^^^^Spacious 3 bedroom villa with pool in Vale da Telha, Aljezur, 8670-156^1^^^^^^^1^N^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~
12345_A184ALZ^^Lovely 2 bedroom apartment in Aljezur^^^Algarve^^^^^^^^^^^^^Excellent location for this well presented 2 bedroom apartment situated at the entrance of Aljezur. Within walking distance to all the attractions this portuguese village has to offer including many restaurants, shops and just few minutes drive to the fabulous Amoreira beach. The apartment comprises of a spacious lounge/dining room, 2 bedrooms with fitted wardrobes, fully fitted kitchen, bathroom and a possibility to occupy a  55 m2 area of the attic. Apart from the garage in cave there is a parking space provided which is an excellent asset in the summer months. This property is a perfect residental or holiday home  as well having great rental potential.^Excellent location for this well presented 2 bedroom apartment situated at the entrance of Aljezur. Within walking distance to all the attractions this portuguese village has to offer including many restaurants, shops and just few minutes drive to the fabulous Amoreira beach. The apartment comprises of a spacious lounge/dining room, 2 bedrooms with fitted wardrobes, fully fitted kitchen, bathroom and a possibility to occupy a  55 m2 area of the attic. Apart from the garage in cave there is a parking space provided which is an excellent asset in the summer months. This property is a perfect residental or holiday home  as well having great rental potential.<br/><br/><br/>^12345^0^2^140000^0^^^^Lovely 2 bedroom apartment in Aljezur, Aljezur, Algarve^1^^^^^^^1^N^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~

是否可以,仅使用XSLT(!)生成XML文件,例如:

<POST>
<AGENT_REF>12345_ALZ57065V</AGENT_REF>
<ADDRESS_2>Spacious 3 bedroom villa with pool in Vale da Telha</ADDRESS_2>
...
</POST>
<POST>
<AGENT_REF>12345_A184ALZ</AGENT_REF>
...
</POST>

基本上,使用DEFINITION关键字(由^分割)来获取DATA中的值,这些值在吐出时的显示顺序与定义中的数据相同。

谢谢,

3 个答案:

答案 0 :(得分:5)

是的,可以使用<xsl:analyze-string>将非XML数据转换为XML。

答案 1 :(得分:1)

XSLT用于转换XML。由于您发布的第一个数据不是XML,因此无法使用XSLT执行此操作。

如果你想反向,那么 就可以了。

更新:

如果您使用的是XSLT 2.0,请参阅@Max Toro答案。否则这是正确的。

答案 2 :(得分:-1)

XSLT 1.0用于将XML转换为XML,文本或HTML。因此输入必须是有效的XML。虽然这可以在您的示例中通过将您的输入包含在某个根xml标记(例如<root>)并转义某些字符(例如&lt;)而在技术上完成,但这仍然需要您在XSLT中进行字符串解析 - XSLT不是为其设计的。这并非不可能,但它会非常缓慢(而且编写起来很乏味)。

您可以先将输入文件转换为有效的xml(例如使用perl,awk,sed,python,...),以产生如下内容:

<root>
  <definition>
    <item>AGENT_REF</item>
    <item>ADDRESS_1</item>
    <item>ADDRESS_2</item>
    ...
  </definition>
  <data>
    <line>
      <item>12345_ALZ57065V</item>
      <item/>   <!-- important! -->
      <item>Spacious 3 bedroom villa with pool in Vale da Telha</item>
      <item/>
      ...
    </line>
    <line>
      ...
    </line>
    ...
  </data>
</root>

然后使用XSLT将其转换为您想要的结果。

OTOH,当数据已经由字符串处理语言(如perl)处理时,它只是一个非常小的附加步骤来直接生成所需的XML,而根本不使用XSLT。 / p>