无法使用 XSLT 转换 XML

Posted

技术标签:

【中文标题】无法使用 XSLT 转换 XML【英文标题】:Unable to Transform XML using XSLT 【发布时间】:2021-09-19 00:43:37 【问题描述】:

我想使用 xslt 转换 xml。下面是我想要的xml和输出。我只想选择带有属性的问题标签,而不是 QuestionText 或 Answer 标签。我该怎么做呢?我需要创建一个模板来选择问题标签吗?如果是这样,我该怎么做? xml:

<Upsert xmlns="http://talemetry.com/">
      <Locale>fr</Locale>
      <LanguageAware>true</LanguageAware>
      <Question active="true" question_id="NOV22-Question" read_only="false" show_on_profile="true" type="multiple_choice_multi_answer">
        <QuestionText>Please select your list of Achievements</QuestionText>
        <RegEx>[0-9]+</RegEx>
        <Answers sort_by="text">
          <Answer answer_id="BOD" is_correct="true" seq="#">
            <AnswerText>Board of Directors Appointment</AnswerText>
          </Answer>
          <Answer answer_id="CMT" is_correct="true">
            <AnswerText>Certificate of Merit Award</AnswerText>
          </Answer>
          <Answer answer_id="CSS" is_correct="true">
            <AnswerText>Community Service Award</AnswerText>
          </Answer>
          <Answer answer_id="DCN" is_correct="true">
            <AnswerText>Dale Carnegie</AnswerText>
          </Answer>
          <Answer answer_id="DEA" is_correct="true">
            <AnswerText>Dean's Award</AnswerText>
          </Answer>
          <Answer answer_id="DLA" is_correct="true">
            <AnswerText>Distinguished Lectureship Awrd</AnswerText>
          </Answer>
          <Answer answer_id="EDU" is_correct="true">
            <AnswerText>Education Award</AnswerText>
          </Answer>
          <Answer answer_id="HDD" is_correct="true">
            <AnswerText>Honorary Doctorate Degree</AnswerText>
          </Answer>
          <Answer answer_id="MBE" is_correct="true">
            <AnswerText>Member of the British Empire</AnswerText>
          </Answer>
          <Answer answer_id="NMS" is_correct="true">
            <AnswerText>National Merit Scholarship</AnswerText>
          </Answer>
          <Answer answer_id="OBE" is_correct="true">
            <AnswerText>Order of the British Empire</AnswerText>
          </Answer>
          <Answer answer_id="OCA" is_correct="true">
            <AnswerText>Outstanding Contributor Award</AnswerText>
          </Answer>
          <Answer answer_id="PRE" is_correct="true">
            <AnswerText>President's Award</AnswerText>
          </Answer>
          <Answer answer_id="PUB" is_correct="true">
            <AnswerText>Publication Award</AnswerText>
          </Answer>
          <Answer answer_id="RPG" is_correct="true">
            <AnswerText>Research &amp; Publications Grant</AnswerText>
          </Answer>
          <Answer answer_id="RYA" is_correct="true">
            <AnswerText>Retiree of the Year Award</AnswerText>
          </Answer>
          <Answer answer_id="SCL" is_correct="true">
            <AnswerText>Sales Excellence Club</AnswerText>
          </Answer>
          <Answer answer_id="SER" is_correct="true">
            <AnswerText>Service Award</AnswerText>
          </Answer>
          <Answer answer_id="SFT" is_correct="true">
            <AnswerText>Safety Prevention Award</AnswerText>
          </Answer>
          <Answer answer_id="SPY" is_correct="true">
            <AnswerText>Sales Person of the Year</AnswerText>
          </Answer>
          <Answer answer_id="TECHY" is_correct="true">
            <AnswerText>Techy Gold Award</AnswerText>
          </Answer>
          <Answer answer_id="TST" is_correct="true">
            <AnswerText>Toastmasters</AnswerText>
          </Answer>
          <Answer answer_id="VYA" is_correct="true">
            <AnswerText>Volunteer of the Year Award</AnswerText>
          </Answer>
        </Answers>
      </Question>
<Question active="true" question_id="EEO_CommunityNI" read_only="false" show_on_profile="false" type="multiple_choice_single_answer">
        <QuestionText>What is your Community Religion (NI)?</QuestionText>
        <Answers sort_by="text">
          <Answer answer_id="CA" is_correct="true">
            <AnswerText>Catholic</AnswerText>
          </Answer>
          <Answer answer_id="PR" is_correct="true">
            <AnswerText>Protestant</AnswerText>
          </Answer>
          <Answer answer_id="UD" is_correct="true">
            <AnswerText>Undetermined/Other</AnswerText>
          </Answer>
        </Answers>
      </Question>
    </Upsert>

输出:

<?xml version="1.0"?>
<soap:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hr="http://ns.hr-xml.org" xmlns:m="http://peoplesoft.com/HCM/Schema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Header>
    <AuthHeader xmlns="http://talemetry.com/">
      <Username>blah-blah</Username>
      <Password>blah-blah</Password>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <Delete xmlns="http://talemetry.com/">
      <Locale>fr</Locale>
      <LanguageAware>true</LanguageAware>
      <Question active="true" question_id="NOV22-Question" read_only="false" show_on_profile="true" type="multiple_choice_multi_answer">
      <Question active="true" question_id="EEO_CommunityNI" read_only="false" show_on_profile="false" type="multiple_choice_single_answer">
</Delete>
  </soap:Body>
</soap:Envelope>

当前 xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://ns.hr-xml.org/2007-04-15"
    xmlns:hr="http://ns.hr-xml.org/2007-04-15"
    xmlns:tal="http://talemetry.com/"
    exclude-result-prefixes="tal">
    <xsl:template match="/">
        <SOAP-ENV:Envelope xmlns=""
            xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <SOAP-ENV:Header>
                <AuthHeader xmlns="http://talemetry.com/">
                    <Username>$param_cred_api_client_id</Username>
                    <Password>$param_cred_api_client_secret</Password>
                </AuthHeader>
            </SOAP-ENV:Header>
            <SOAP-ENV:Body>       
            <Delete xmlns="http://talemetry.com/">
                <Locale>
                 <xsl:value-of select="tal:Upsert/tal:Locale"/>
                </Locale>
                <LanguageAware>true</LanguageAware>                
                    <xsl:copy-of select="tal:Upsert/tal:Question"></xsl:copy-of>              
            </Delete>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
    </xsl:template>
</xsl:stylesheet>

【问题讨论】:

请添加您当前的 XSLT,以便我们修复它,而不必从头开始为您编写代码。 @michael.hor257k 刚刚将其添加到问题中 【参考方案1】:

AFAICT,你想做这样的事情:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tal="http://talemetry.com/"
exclude-result-prefixes="tal">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/tal:Upsert">
    <soap:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hr="http://ns.hr-xml.org" xmlns:m="http://peoplesoft.com/HCM/Schema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soap:Header>
            <AuthHeader xmlns="http://talemetry.com/">
                <Username>blah-blah</Username>
                <Password>blah-blah</Password>
            </AuthHeader>
        </soap:Header>
        <soap:Body>
            <Delete xmlns="http://talemetry.com/">
                <xsl:copy-of select="tal:Locale | tal:LanguageAware"/>
                <xsl:for-each select="tal:Question">
                    <xsl:copy>
                        <xsl:copy-of select="@*"/>
                    </xsl:copy>
                </xsl:for-each>
            </Delete>
        </soap:Body>
    </soap:Envelope>
</xsl:template>

</xsl:stylesheet>

【讨论】:

以上是关于无法使用 XSLT 转换 XML的主要内容,如果未能解决你的问题,请参考以下文章

XSLT:无法将操作数转换为“结果树片段”

XSLT 3.0 - 无法在 XSLT 3.0 xml-to-json() 中获取对象数组

XSLT for-each 无法提取值

使用命名空间时无法在 XSLT 中复制和修改属性

如何使用 XSLT 将 JSON 转换为 XML?

XSLT学习通过JavaScript转化xml