You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助使用XSLT将指定HTML转换为XML

用XSLT将PHP数组格式的HTML转换为结构化XML

嘿,我来帮你搞定这个转换问题!首先得明确:你输入的HTML里,<body>中的内容其实是PHP print_r() 输出的数组文本,不是标准的HTML元素,所以我们需要先把这个纯文本解析成结构化数据,再转换成目标XML。下面是基于XSLT 2.0的解决方案(XSLT 2.0的字符串处理能力更方便,如果你需要XSLT 1.0的版本,我可以再调整):

第一步:XSLT转换代码

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

  <!-- 匹配根节点,开始处理 -->
  <xsl:template match="/html">
    <Root>
      <!-- 提取body内的文本,清理多余空格和换行 -->
      <xsl:variable name="arrayText" select="normalize-space(body/text())"/>
      <!-- 拆分顶层数组的键值对 -->
      <xsl:variable name="topPairs" select="tokenize(substring-before(substring-after($arrayText, 'Array ( '), ' )'), '\[([^\]]+)\] => ')"/>
      
      <!-- 处理Value和Head这两个非数组项 -->
      <xsl:for-each select="$topPairs[position() mod 2 = 1]">
        <xsl:variable name="key" select="."/>
        <xsl:variable name="value" select="$topPairs[position() + current()/position()]"/>
        <xsl:if test="$key = 'Value' or $key = 'Head'">
          <xsl:element name="{$key}">
            <xsl:value-of select="$value"/>
          </xsl:element>
        </xsl:if>
      </xsl:for-each>

      <!-- 处理嵌套的Description、Price、Quantity数组,配对生成Item -->
      <Items>
        <!-- 提取Description数组的元素 -->
        <xsl:variable name="descItems" select="tokenize(substring-before(substring-after($arrayText, '[Description] => Array ( '), ' )'), '\[([0-9]+)\] => ')"/>
        <!-- 提取Price数组的元素 -->
        <xsl:variable name="priceItems" select="tokenize(substring-before(substring-after($arrayText, '[Price] => Array ( '), ' )'), '\[([0-9]+)\] => ')"/>
        <!-- 提取Quantity数组的元素 -->
        <xsl:variable name="qtyItems" select="tokenize(substring-before(substring-after($arrayText, '[Quantity] => Array ( '), ' )'), '\[([0-9]+)\] => ')"/>

        <!-- 循环生成每个Item -->
        <xsl:for-each select="$descItems[position() mod 2 = 1]">
          <xsl:variable name="index" select="position()"/>
          <Item>
            <Description>
              <xsl:value-of select="$descItems[$index * 2]"/>
            </Description>
            <Price>
              <xsl:value-of select="$priceItems[$index * 2]"/>
            </Price>
            <Quantity>
              <xsl:value-of select="$qtyItems[$index * 2]"/>
            </Quantity>
          </Item>
        </xsl:for-each>
      </Items>
    </Root>
  </xsl:template>
</xsl:stylesheet>

第二步:转换后的期望输出

运行上面的XSLT后,会生成如下结构化XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <Value>123</Value>
  <Head>456</Head>
  <Items>
    <Item>
      <Description>ABC</Description>
      <Price>123</Price>
      <Quantity/>
    </Item>
    <Item>
      <Description>DEF</Description>
      <Price>456</Price>
      <Quantity/>
    </Item>
    <Item>
      <Description>GHI</Description>
      <Price>789</Price>
      <Quantity/>
    </Item>
  </Items>
</Root>

注意事项

  • 这个方案依赖于print_r()输出的数组格式固定,如果你的输入文本格式有变化(比如空格、换行不同),可能需要调整tokenize()的参数或者字符串截取逻辑。
  • 如果只能使用XSLT 1.0,由于没有原生的字符串拆分函数,需要用递归模板来实现字符串拆分,逻辑会更繁琐,如果你需要的话可以告诉我,我再提供对应的版本。

内容的提问来源于stack exchange,提问作者Sendhil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:46:23