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

新手求助:如何使用XSLT将XML 1转换为XML 2?

Hey there! Since you're new to XSLT, let's walk through how to transform your source XML (XML 1) into your target XML (XML 2). First, let's lay out the partial source XML you shared:

<?xml version="1.0" encoding="utf-8"?>
<root type="array">
  <item type="object">
    <a:item xmlns:a="item" item="@id" type="string">_:genid1</a:item>
    <a:item xmlns:a="item" item="@type" type="array">
      <item type="string">http://www.w3.org/2000/01/rdf-schema#Datatype</item>
    </a:item>
    <a:item xmlns:a="item" item="http://www.w3.org/2002/07/owl#oneOf" type="array">
      <!-- Assume this contains more <item> elements with values -->
    </a:item>
  </item>
</root>

I’m guessing your target XML (XML 2) is a more standard RDF/OWL structure, like this:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:owl="http://www.w3.org/2002/07/owl#">
  <rdfs:Datatype rdf:about="_:genid1">
    <owl:oneOf>
      <rdf:Seq>
        <!-- Your values from the source's owl:oneOf array here -->
      </rdf:Seq>
    </owl:oneOf>
  </rdfs:Datatype>
</rdf:RDF>

Here’s an XSLT template that will handle this transformation, with comments explaining each step so you can follow along:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="item"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
                xmlns:owl="http://www.w3.org/2002/07/owl#">
  <!-- Make sure the output is nicely formatted XML -->
  <xsl:output method="xml" encoding="utf-8" indent="yes"/>

  <!-- Start with the root element of your source XML -->
  <xsl:template match="/root">
    <rdf:RDF>
      <!-- Process each object item in the source array -->
      <xsl:apply-templates select="item[@type='object']"/>
    </rdf:RDF>
  </xsl:template>

  <!-- Convert each source object into an RDF datatype node -->
  <xsl:template match="item[@type='object']">
    <!-- Grab the ID value from the @id item -->
    <xsl:variable name="resourceId" select="a:item[@item='@id']/text()"/>
    <!-- Grab the datatype URI from the @type array -->
    <xsl:variable name="datatypeUri" select="a:item[@item='@type']/item/text()"/>
    
    <!-- Dynamically create the correct RDFS/OWL element using the URI -->
    <xsl:element name="{local-name($datatypeUri)}" namespace="{namespace-uri-from-QName($datatypeUri)}">
      <xsl:attribute name="rdf:about">
        <xsl:value-of select="$resourceId"/>
      </xsl:attribute>
      
      <!-- Process the owl:oneOf property -->
      <xsl:apply-templates select="a:item[@item='http://www.w3.org/2002/07/owl#oneOf']"/>
    </xsl:element>
  </xsl:template>

  <!-- Convert the owl:oneOf array into a standard RDF sequence -->
  <xsl:template match="a:item[@item='http://www.w3.org/2002/07/owl#oneOf']">
    <owl:oneOf>
      <rdf:Seq>
        <!-- Turn each item in the array into an RDF list item -->
        <xsl:apply-templates select="item"/>
      </rdf:Seq>
    </owl:oneOf>
  </xsl:template>

  <!-- Format each individual value from the owl:oneOf array -->
  <xsl:template match="a:item[@item='http://www.w3.org/2002/07/owl#oneOf']/item">
    <rdf:li>
      <xsl:value-of select="text()"/>
    </rdf:li>
  </xsl:template>

  <!-- Ignore any extra text or attributes we don't need in the output -->
  <xsl:template match="text()|@*"/>
</xsl:stylesheet>

Key Notes for You (As a New XSLT User):

  • Namespaces: We declare all the namespaces used in both the source and target XML so XSLT can properly recognize elements.
  • Dynamic Elements: The <xsl:element> tag lets us create elements based on the URI in your source (so if your @type was a different OWL/RDFS term, it would adjust automatically).
  • Templates: Each template handles a specific part of the XML structure—this keeps the code organized and easy to modify if your target XML changes.

If your actual XML 2 has a different structure, or if you can share the full XML 1 content, I can tweak this template to fit exactly what you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:37:33