无法使用 XSLT 转换 XML
Unable to Transform XML using XSLT
我想使用 xslt 转换 xml。下面是 xml 和我想要的输出。我只想 select 问题标签及其属性,而不是问题文本或答案标签。我该怎么做呢?我是否需要为 select 个问题标签创建一个模板?如果是这样,我该怎么做?
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 & 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>
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。下面是 xml 和我想要的输出。我只想 select 问题标签及其属性,而不是问题文本或答案标签。我该怎么做呢?我是否需要为 select 个问题标签创建一个模板?如果是这样,我该怎么做? 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 & 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>
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>