Click here to Skip to main content
14,266,990 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to breakdown an xml document into two. This is basically by accessing the second xml file as external entity. Originally the full document looks as follows:
<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>
    <par1>Don't forget me this weekend!</par1>
   </body>
</note>


this document is saved with file name
mymessage.txt
and i can access the content of par1 tag from powershell as

$xmlFileName = 'E:\mymessage.txt'
[xml]$xmlDoc = Get-Content $xmlFileName
Write-Host $xmlDoc.note.body.par1


This works fine.

What I have tried:

Now i want to split the file into two and I put the body part in separate file called
mymessage_body.txt
and it looks

<?xml version="1.0" encoding="UTF-8"?>
<DOCTYPE body [
  <!ELEMENT body (par1)>
  <!ELEMENT par1 (#PCDATA)>
]>
  
<body>
  <par1>Don't forget me this weekend!</par1>
</body>



Similarly i put the header part in another file (called
mymessage_header.txt
) and it looks

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
  <!ELEMENT note ANY>
  <!ELEMENT to (#PCDATA)>
  <!ELEMENT from (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ENTITY body SYSTEM "mymessage_body.txt">
]>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
   &body;
</note>


Now i tried to access the content of par1 from powershell as shown above it returns nothing. I want to know weather the external entity is properly loaded or not. Would you please forward your suggestion in resolving this? Thanks!
Posted
Comments
0x01AA 31-May-19 7:52am
   
When I reduce mymessage_body.txt to "<par1>Don't forget me this weekend!</par1>" then it works for me.
getrelax 31-May-19 11:10am
   
I reduced the content of mymessage_body.txt to what you suggested and i used the powershell command
$xmlFileName = 'E:\mymessage.txt'
[xml]$xmlDoc = Get-Content $xmlFileName
Write-Host $xmlDoc.note.par1
but still it didn't return the content of par1. Would you please share the powershell command you used to access it.
0x01AA 31-May-19 13:04pm
   
Sorry for my lazy 'test'. I used only XMLNotepad for 'test'. Now trying it with powershell I see it does not work and it looks it is not possible. See here the not accepted answer: powershell - How to include a XML snippet into a XML document - Stack Overflow[^]

[Edit]
And much more strange: As soon the xml has an external reference, also the properties like 'to', 'from' from the main xml are not longer available anymore by powershell...
0x01AA 31-May-19 15:54pm
   
FYI: Try hard to find a way to solve it, but also with following no success :(

$resolver = New-Object System.Xml.XmlUrlResolver
$resolver.Credentials= [System.Net.CredentialCache]::DefaultCredentials
$xmlDoc1 = New-Object System.Xml.XmlDocument
$xmlDoc1.XmlResolver = $resolver
$xmlDoc1.Load(".\mymessage_header1.txt.xml")
Write-Host "Start Test1"
Write-Host $xmlDoc1.note.from
Write-Host $xmlDoc1.note.par1
Write-Host "Test1 Done"
0x01AA 1-Jun-19 8:44am
   
A small step forward:

$resolver = New-Object System.Xml.XmlUrlResolver
$resolver.Credentials= [System.Net.CredentialCache]::DefaultNetworkCredentials
$xmlDoc1 = New-Object System.Xml.XmlDocument
$xmlDoc1.XmlResolver = $resolver
$xmlDoc1.Load(".\mymessage_header1.txt.xml")

$xmlDoc1.note | Format-Table -AutoSize
$notes = $xmlDoc1.selectNodes("//note//par1")
$notes
Write-Output $notes


The last $notes and Write-Output $notes do output the expected string "Don't forget me this weekend!" while Write-Host $notes outputs a blank string
Gerry Schmitz 31-May-19 22:42pm
   
Makes me think of using a noodle for a fishing rod. Maybe try a "higher-level language" that you can use long term.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100